欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python面向?qū)ο缶幊蹋ㄈ?/h1>
 更新時(shí)間:2022年05月30日 09:08:14   作者:springsnow  
本文詳細(xì)講解了Python的面向?qū)ο缶幊?,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Python面向?qū)ο缶幊蹋ㄒ唬?/a>

Python面向?qū)ο缶幊蹋ǘ?/a>

Python面向?qū)ο缶幊蹋ㄈ?/a>

一、isinstance和issubclass

  • type():不會(huì)認(rèn)為子類實(shí)例是一種父類類型;
  • isinstance():認(rèn)為子類實(shí)例是一種父類類型。
  • issubclass():判斷是否為其子類。
class Foo(object):
    pass


class Bar(Foo):
    pass


print(type(Foo()) == Foo)
# True

print(type(Bar()) == Foo)
# False

# isinstance參數(shù)為對(duì)象和類
print(isinstance(Bar(), Foo))
# True


print(issubclass(Bar, Foo))
# True

print(issubclass(Foo, object))
# True

二、反射(hasattr和getattr和setattr和delattr)

1、反射在類中的使用

反射就是通過字符串來操作類或者對(duì)象的屬性。反射本質(zhì)就是在使用內(nèi)置函數(shù),其中反射有以下四個(gè)內(nèi)置函數(shù):

  • hasattr:判斷一個(gè)方法是否存在與這個(gè)類中
  • getattr:根據(jù)字符串去獲取obj對(duì)象里的對(duì)應(yīng)的方法的內(nèi)存地址,加"()"括號(hào)即可執(zhí)行
  • setattr:通過setattr將外部的一個(gè)函數(shù)綁定到實(shí)例中
  • delattr:刪除一個(gè)實(shí)例或者類中的方法
class People:
    country = 'China'

    def __init__(self, name):
        self.name = name

    def eat(self):
        print('%s is eating' % self.name)


peo1 = People('nick')

print(hasattr(peo1, 'eat'))  # peo1.eat
# True

print(getattr(peo1, 'eat'))  # peo1.eat
# >

print(getattr(peo1, 'xxxxx', None))
# None

setattr(peo1, 'age', 18)  # peo1.age=18
print(peo1.age)
# 18

print(peo1.__dict__)
# {'name': 'egon', 'age': 18}

delattr(peo1, 'name')  # del peo1.name
print(peo1.__dict__)
# {'age': 18}

2、反射在模塊中的使用

動(dòng)態(tài)導(dǎo)入一個(gè)模塊__import__,并且動(dòng)態(tài)輸入函數(shù)名然后執(zhí)行相應(yīng)功能。

注意:getattr,hasattr,setattr,delattr對(duì)模塊的修改都在內(nèi)存中進(jìn)行,并不會(huì)影響文件中真實(shí)內(nèi)容。

# dynamic.py
imp = input("請(qǐng)輸入模塊:")
commons = __import__(imp)  # 等價(jià)于import imp
# commons = __import__(imp, fromlist=True)  # 模塊名可能不是在本級(jí)目錄中存放著,改用這種方式就能導(dǎo)入成功
inp_func = input("請(qǐng)輸入要執(zhí)行的函數(shù):")
f = getattr(commons, inp_func, None)  # 作用:從導(dǎo)入模塊中找到你需要調(diào)用的函數(shù)inp_func,然后返回一個(gè)該函數(shù)的引用.沒有找到就煩會(huì)None
f()  # 執(zhí)行該函數(shù)

r = hasattr(commons, 'age')  # 判斷某個(gè)函數(shù)或者變量是否存在
print(r)

setattr(commons, 'age', 18)  # 給commons模塊增加一個(gè)全局變量age = 18,創(chuàng)建成功返回none

setattr(commons, 'age', lambda a: a + 1)  # 給模塊添加一個(gè)函數(shù)

delattr(commons, 'age')  # 刪除模塊中某個(gè)變量或者函數(shù)

3、實(shí)例:基于反射機(jī)制模擬web框架路由

需求:比如我們輸入<www.xxx.com/commons/f1> ,返回f1的結(jié)果。

# 動(dòng)態(tài)導(dǎo)入模塊,并執(zhí)行其中函數(shù)
url = input("url: ")

target_host,target_module, target_func = url.split('/')
m = __import__('aaa.' + target_module, fromlist=True)

inp = url.split("/")[-1]  # 分割url,并取出url最后一個(gè)字符串
if hasattr(m, inp):  # 判斷在commons模塊中是否存在inp這個(gè)字符串
    inp= getattr(m, inp)  # 獲取inp的引用
    inp()  # 執(zhí)行
else:
    print("404")

三、__getattr__、__setattr__和__delattr__和__getattribute__事件

  • __getattr__:只有在使用點(diǎn)調(diào)用屬性且屬性不存在的時(shí)候才會(huì)觸發(fā)。比較有用
  • __delattr__:刪除屬性的時(shí)候會(huì)觸發(fā)
  • __setattr__:添加/修改屬性會(huì)觸發(fā)它的執(zhí)行
    當(dāng)你自己寫__getattr__、__delattr__、__setattr__方法,系統(tǒng)會(huì)調(diào)用你寫的方法,如果沒寫,系統(tǒng)調(diào)用默認(rèn)
class Foo:
    x = 1

    def __init__(self, y):
        self.y = y

    def __getattr__(self, item):
        print('----> from getattr:你找的屬性不存在')

    def __setattr__(self, key, value):
        print('----> from setattr')
        # self.key = value  # 這就無限遞歸了,你好好想想
        # self.__dict__[key] = value  # 應(yīng)該使用它

    def __delattr__(self, item):
        print('----> from delattr')
        # del self.item  # 無限遞歸了
        self.__dict__.pop(item)


f1 = Foo(10)
# ----> from setattr
print(f1.__dict__ )  # 因?yàn)槟阒貙懥薩_setattr__,凡是賦值操作都會(huì)觸發(fā)它的運(yùn)行,你啥都沒寫,就是根本沒賦值。除非你直接操作屬性字典,否則永遠(yuǎn)無法賦值
# {}

f1.z = 3
# ----> from setattr
print(f1.__dict__)
# {}

f1.__dict__['a'] = 3  # 我們可以直接修改屬性字典,來完成添加/修改屬性的操作(不會(huì)觸發(fā)__setattr__)
del f1.a
# ----> from delattr
 
print(f1.__dict__)
# {}

__getattribute__

查找屬性無論是否存在,都會(huì)執(zhí)行。

class Foo:
    def __init__(self, x):
        self.x = x

    def __getattribute__(self, item):
        print('不管是否存在,我都會(huì)執(zhí)行')


f1 = Foo(10)

f1.x
# 不管是否存在,我都會(huì)執(zhí)行

f1.xxxxxx
# 不管是否存在,我都會(huì)執(zhí)行

當(dāng)__getattribute__與__getattr__同時(shí)存在,只會(huì)執(zhí)行__getattrbute__,除非__getattribute__在執(zhí)行過程中拋出異常AttributeError

class Foo:
    def __init__(self, x):
        self.x = x

    def __getattr__(self, item):
        print('執(zhí)行的是我')
        # return self.__dict__[item]

    def __getattribute__(self, item):
        print('不管是否存在,我都會(huì)執(zhí)行')
        raise AttributeError('哈哈')


f1 = Foo(10)

f1.x
# 不管是否存在,我都會(huì)執(zhí)行
# 執(zhí)行的是我

f1.xxxxxx
# 不管是否存在,我都會(huì)執(zhí)行
# 執(zhí)行的是我

四、__setitem__和__getitem和__delitem__

  • __setitem__:中括號(hào)賦值時(shí)觸發(fā)
  • __getitem__:中括號(hào)取值時(shí)觸發(fā)
  • __delitem__:中括號(hào)刪除時(shí)觸發(fā)
  • __delattr__:.刪除時(shí)觸發(fā)
class Foo:
    def __init__(self, name):
        self.name = name

    def __getitem__(self, item):
        print('getitem執(zhí)行', self.__dict__[item])

    def __setitem__(self, key, value):
        print('setitem執(zhí)行')
        self.__dict__[key] = value

    def __delitem__(self, key):
        print('del obj[key]時(shí),delitem執(zhí)行')
        self.__dict__.pop(key)

    def __delattr__(self, item):
        print('del obj.key時(shí),delattr執(zhí)行')
        self.__dict__.pop(item)


f1 = Foo('sb')

f1['age'] = 18
# setitem執(zhí)行

f1['age1'] = 19
# setitem執(zhí)行

f1['age']
# getitem執(zhí)行 18

f1['name'] = 'tank'
# setitem執(zhí)行

del f1.age1
# del obj.key時(shí),delattr執(zhí)行

del f1['age']
# del obj[key]時(shí),delitem執(zhí)行

print(f1.__dict__)
# {'name': 'tank'}

五、__format__:自定制格式化字符串

date_dic = {
    'ymd': '{0.year}:{0.month}:{0.day}',
    'dmy': '{0.day}/{0.month}/{0.year}',
    'mdy': '{0.month}-{0.day}-{0.year}',
}


class Date:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day

    def __format__(self, format_spec):
        # 默認(rèn)打印ymd的{0.year}:{0.month}:{0.day}格式
        if not format_spec or format_spec not in date_dic:
            format_spec = 'ymd'
        fmt = date_dic[format_spec]
        return fmt.format(self)


d1 = Date(2016, 12, 29)

print(format(d1))
# 2016:12:29

print('{:mdy}'.format(d1))
# 12-29-2016

六、__del__:析構(gòu)方法

會(huì)在對(duì)象被刪除之前自動(dòng)觸發(fā)

class People:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.f = open('test.txt', 'w', encoding='utf-8')

    def __del__(self):
        print('run======>')
        # 做回收系統(tǒng)資源相關(guān)的事情
        self.f.close()


obj = People('egon', 18)

del obj  # del obj會(huì)間接刪除f的內(nèi)存占用,但是還需要自定制__del__刪除文件的系統(tǒng)占用
# run=-====>

七、__slots__

使用點(diǎn)來訪問屬性本質(zhì)就是在訪問類或者對(duì)象的__dict__屬性字典(類的字典是共享的,而每個(gè)實(shí)例的是獨(dú)立的)。

__slots__是一個(gè)類變量,變量值可以是列表,元祖,或者可迭代對(duì)象,也可以是一個(gè)字符串(意味著所有實(shí)例只有一個(gè)數(shù)據(jù)屬性)

字典會(huì)占用大量內(nèi)存,如果你有一個(gè)屬性很少的類,但是有很多實(shí)例,為了節(jié)省內(nèi)存可以使用__slots__取代實(shí)例的__dict__。

class Foo:
    __slots__ = 'x'


f1 = Foo()
f1.x = 1
f1.y = 2  # 報(bào)錯(cuò)
print(f1.__slots__ )  # f1不再有__dict__

當(dāng)你定義__slots__后,__slots__就會(huì)為實(shí)例使用一種更加緊湊的內(nèi)部表示。使用__slots__后不能再給實(shí)例添加新的屬性了,只能使用在__slots__中定義的那些屬性名。

注意:__slots__的很多特性都依賴于普通的基于字典的實(shí)現(xiàn)。另外,定義了__slots__后的類不再支持一些普通類特性了,比如多繼承。

大多數(shù)情況下,你應(yīng)該只在那些經(jīng)常被使用到的用作數(shù)據(jù)結(jié)構(gòu)的類上定義__slots__比如在程序中需要?jiǎng)?chuàng)建某個(gè)類的幾百萬個(gè)實(shí)例對(duì)象 。

class Bar:
    __slots__ = ['x', 'y']


n = Bar()
n.x, n.y = 1, 2
n.z = 3  # 報(bào)錯(cuò)

八、__doc__:返回類的注釋信息

class Foo:
    '我是描述信息'
    pass


print(Foo.__doc__)
# 我是描述信息

該屬性無法被繼承

class Foo:
    '我是描述信息'
    pass


class Bar(Foo):
    pass


print(Bar.__doc__)  # 該屬性無法繼承給子類
# None

九、__call__:會(huì)在調(diào)用對(duì)象時(shí)自動(dòng)觸發(fā)。

構(gòu)造方法的執(zhí)行是由創(chuàng)建對(duì)象觸發(fā)的,即:對(duì)象 = 類名() ;而對(duì)于 __call__ 方法的執(zhí)行是由對(duì)象后加括號(hào)觸發(fā)的,即:對(duì)象() 或者 類()()

class Foo:
    def __init__(self):
        print('__init__觸發(fā)了')

    def __call__(self, *args, **kwargs):
        print('__call__觸發(fā)了')


obj = Foo()  # 執(zhí)行 __init__
# __init__觸發(fā)了

obj()  # 執(zhí)行 __call__
# __call__

十、__init__和__new__:類構(gòu)造器

__new__方法的第一個(gè)參數(shù)是這個(gè)類,而其余的參數(shù)會(huì)在調(diào)用成功后全部傳遞給__init__方法初始化。

__new__方法(第一個(gè)執(zhí)行)先于__init__方法執(zhí)行:

class A:
    pass


class B(A):
    def __new__(cls):
        print("__new__方法被執(zhí)行")
        return super().__new__(cls)

    def __init__(self):
        print("__init__方法被執(zhí)行")


b = B()

# __new__方法被執(zhí)行
# __init__方法被執(zhí)行

絕大多數(shù)情況下,我們都不需要自己重寫__new__方法,但在當(dāng)繼承一個(gè)不可變的類型(例如str類,int類等)時(shí),它的特性就尤顯重要了。我們舉下面這個(gè)例子:

# 1、使用init的情況:
class CapStr1(str):
    def __init__(self, string):
        string = string.upper()


a = CapStr1("I love China!")
print(a)
# I love China!   無變化 !?。。。。。?



# 2、使用__new__的情況
class CapStr2(str):
    def __new__(cls, string):
        string = string.upper()
        return super().__new__(cls, string)


a = CapStr2("I love China!")
print(a)
# I LOVE CHINA!

十一、__str__和__repr__

__str__:執(zhí)行str函數(shù)或print函數(shù)觸發(fā)

class Foo:
    def __init__(self, name, age):
        """對(duì)象實(shí)例化的時(shí)候自動(dòng)觸發(fā)"""
        self.name = name
        self.age = age

    def __str__(self):
        print('打印的時(shí)候自動(dòng)觸發(fā),但是其實(shí)不需要print即可打印')
        return f'{self.name}:{self.age}'  # 如果不返回字符串類型,則會(huì)報(bào)錯(cuò)


obj = Foo('nick', 18)
print(obj)  # obj.__str__() # 打印的時(shí)候就是在打印返回值
# 打印的時(shí)候自動(dòng)觸發(fā),但是其實(shí)不需要print即可打印
# nick:18

__repr__:執(zhí)行repr函數(shù)或者交互式解釋器觸發(fā)

  • 如果__str__沒有被定義,那么就會(huì)使用__repr__來代替輸出。
  • 注意:這倆方法的返回值必須是字符串,否則拋出異常。
class School:
    def __init__(self, name, addr, type):
        self.name = name
        self.addr = addr
        self.type = type

    def __repr__(self):
        return 'School(%s,%s)' % (self.name, self.addr)

    def __str__(self):
        return '(%s,%s)' % (self.name, self.addr)


s1 = School('oldboy1', '北京', '私立')
print('from repr: ', repr(s1))
# from repr:  School(oldboy1,北京)

print('from str: ', str(s1))
# from str:  (oldboy1,北京)

print(s1)
# (oldboy1,北京)

s1  # jupyter屬于交互式
# School(oldboy1,北京)

十二、__module__和__class__

  • __module__ 表示當(dāng)前操作的對(duì)象在那個(gè)模塊
  • __class__表示當(dāng)前操作的對(duì)象的類是什么
# lib/aa.py
class C:
    def __init__(self):
        self.name = 'SB'


# index.py
from lib.aa import C

obj = C()
print(obj.__module__)  # 輸出 lib.aa,即:輸出模塊
print(obj.__class__)  # 輸出 lib.aa.C,即:輸出類

十三、實(shí)現(xiàn)文件上下文管理(__enter__和__exit__)

with語句,即上下文管理協(xié)議,為了讓一個(gè)對(duì)象兼容with語句,必須在這個(gè)對(duì)象的類中聲明__enter__和__exit__方法。

  • 使用with語句的目的就是把代碼塊放入with中執(zhí)行,with結(jié)束后,自動(dòng)完成清理工作,無須手動(dòng)干預(yù)
  • 在需要管理一些資源比如文件,網(wǎng)絡(luò)連接和鎖的編程環(huán)境中,可以在__exit__中定制自動(dòng)釋放資源的機(jī)制,你無須再去關(guān)系這個(gè)問題,這將大有用處。

__exit__()中的三個(gè)參數(shù)分別代表異常類型,異常值和追溯信息。with語句中代碼塊出現(xiàn)異常,則with后的代碼都無法執(zhí)行。

class Open:
    def __init__(self, name):
        self.name = name

    def __enter__(self):
        print('出現(xiàn)with語句,對(duì)象的__enter__被觸發(fā),有返回值則賦值給as聲明的變量')

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('with中代碼塊執(zhí)行完畢時(shí)執(zhí)行我啊')
        print(exc_type)
        print(exc_val)
        print(exc_tb)


try:
    with Open('a.txt') as f:
        print('=====>執(zhí)行代碼塊')
        raise AttributeError('***著火啦,救火啊***')
except Exception as e:
    print(e)

# 出現(xiàn)with語句,對(duì)象的__enter__被觸發(fā),有返回值則賦值給as聲明的變量
# =====>執(zhí)行代碼塊
# with中代碼塊執(zhí)行完畢時(shí)執(zhí)行我啊
# <class 'AttributeError'>
# ***著火啦,救火啊***
#
# ***著火啦,救火啊***

如果__exit()返回值為True,那么異常會(huì)被清空,就好像啥都沒發(fā)生一樣,with后的語句正常執(zhí)行。

class Open:
    def __init__(self, name):
        self.name = name

    def __enter__(self):
        print('出現(xiàn)with語句,對(duì)象的__enter__被觸發(fā),有返回值則賦值給as聲明的變量')

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('with中代碼塊執(zhí)行完畢時(shí)執(zhí)行我啊')
        print(exc_type)
        print(exc_val)
        print(exc_tb)
        return True


with Open('a.txt') as f:
    print('=====>執(zhí)行代碼塊')
    raise AttributeError('***著火啦,救火啊***')
print('0' * 100)  # ------------------------------->會(huì)執(zhí)行

# 出現(xiàn)with語句,對(duì)象的__enter__被觸發(fā),有返回值則賦值給as聲明的變量
# =====>執(zhí)行代碼塊
# with中代碼塊執(zhí)行完畢時(shí)執(zhí)行我啊
# <class 'AttributeError'>
# ***著火啦,救火啊***
#
# 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

#

模擬open

class Open:
    def __init__(self, filepath, mode='r', encoding='utf-8'):
        self.filepath = filepath
        self.mode = mode
        self.encoding = encoding

    def __enter__(self):
        # print('enter')
        self.f = open(self.filepath, mode=self.mode, encoding=self.encoding)
        return self.f

    def __exit__(self, exc_type, exc_val, exc_tb):
        # print('exit')
        self.f.close()
        return True

    def __getattr__(self, item):
        return getattr(self.f, item)


with Open('a.txt', 'w') as f:
    print(f)
    f.write('aaaaaa')
    f.wasdf  #拋出異常,交給__exit__處理

# <_io.TextIOWrapper name='a.txt' mode='w' encoding='utf-8'>

十四、描述符(__get__和__set__和__delete__)

描述符是可以實(shí)現(xiàn)大部分python類特性中的底層魔法,包括@classmethod,@staticmethd,@property甚至是__slots__屬性。

描述符是很多高級(jí)庫和框架的重要工具之一,描述符通常是使用到裝飾器或者元類的大型框架中的一個(gè)組件。

描述符本質(zhì)就是一個(gè)新式類,在這個(gè)新式類中,至少實(shí)現(xiàn)了__get__(),__set__(),__delete__()中的一個(gè),這也被稱為描述符協(xié)議

  • __get__():調(diào)用一個(gè)屬性時(shí),觸發(fā)
  • __set__():為一個(gè)屬性賦值時(shí),觸發(fā)
  • __delete__():采用del刪除屬性時(shí),觸發(fā)

描述符的作用是用來代理另外一個(gè)類的屬性的。包含這三個(gè)方法的新式類稱為描述符,由這個(gè)類產(chǎn)生的實(shí)例進(jìn)行屬性的調(diào)用/賦值/刪除,并不會(huì)觸發(fā)這三個(gè)方法

class Foo:
    def __get__(self, instance, owner):
        print('觸發(fā)get')

    def __set__(self, instance, value):
        print('觸發(fā)set')

    def __delete__(self, instance):
        print('觸發(fā)delete')


f1 = Foo()
f1.name = 'nick'
f1.name
del f1.name
#無任何輸出結(jié)果?。。?/pre>

必須把描述符定義成這個(gè)類的類屬性,不能定義到構(gòu)造函數(shù)中。

class ST:
    """描述符Str"""

    def __get__(self, instance, owner):
        print('Str調(diào)用')

    def __set__(self, instance, value):
        print('Str設(shè)置...')

    def __delete__(self, instance):
        print('Str刪除...')


class IN:
    """描述符Int"""

    def __get__(self, instance, owner):
        print('Int調(diào)用')

    def __set__(self, instance, value):
        print('Int設(shè)置...')

    def __delete__(self, instance):
        print('Int刪除...')


class People:
    name = ST()
    age = IN()

    def __init__(self, name, age):  # name被ST類代理,age被IN類代理
        self.name = name
        self.age = age


p1 = People('alex', 18)
# Str設(shè)置...
# Int設(shè)置...

p1.name  # Str調(diào)用
p1.name = 'nick'  # Str設(shè)置...
del p1.name  # Str刪除...

p1.age  # Int調(diào)用
p1.age = 18  # Int設(shè)置...
del p1.age  # Int刪除...

print(p1.__dict__)  # {}
print(People.__dict__)
# {'__module__': '__main__', 'name': <__main__.ST object at 0x0000000002167490>, 'age': <__main__.IN object at 0x000000000234A700>, '__init__': , '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}

print(type(p1) == People)  # True
print(type(p1).__dict__ == People.__dict__)  # True

1、使用描述符

眾所周知,python是弱類型語言,即參數(shù)的賦值沒有類型限制,下面我們通過描述符機(jī)制來實(shí)現(xiàn)類型限制功能。

class Typed:
    def __init__(self, name, expected_type):
        self.name = name
        self.expected_type = expected_type

    def __get__(self, instance, owner):
        print('get--->', instance, owner)
if instance is None:
return self
return instance.__dict__[self.name]

    def __set__(self, instance, value):
        print('set--->', instance, value)
       if not isinstance(value, self.expected_type):
raise TypeError('Expected %s' % str(self.expected_type))
        instance.__dict__[self.name] = value

    def __delete__(self, instance):
        print('delete--->', instance)
        instance.__dict__.pop(self.name)


class People:
    name = Typed('name', str)
    age = Typed('name', int)
    salary = Typed('name', float)

    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary


try:
    p1 = People(123, 18, 3333.3)
except Exception as e:
    print(e)
# set---> <__main__.People object at 0x1082c7908> 123
# Expected <class 'str'>

try:
    p1 = People('nick', '18', 3333.3)
except Exception as e:
    print(e)
# set---> <__main__.People object at 0x1078dd438> nick
# set---> <__main__.People object at 0x1078dd438> 18
# Expected <class 'int'>

p1 = People('nick', 18, 3333.3)
# set---> <__main__.People object at 0x1081b3da0> nick
# set---> <__main__.People object at 0x1081b3da0> 18
# set---> <__main__.People object at 0x1081b3da0> 3333.3

2、類的裝飾器:無參

def decorate(cls):
    print('類的裝飾器開始運(yùn)行啦------>')
    return cls


@decorate  # 無參:People = decorate(People)
class People:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary


p1 = People('nick', 18, 3333.3)

# 類的裝飾器開始運(yùn)行啦------>

3、類的裝飾器:有參

def typeassert(**kwargs):
    def decorate(cls):
        print('類的裝飾器開始運(yùn)行啦------>', kwargs)
        return cls

    return decorate


@typeassert( name=str, age=int, salary=float)  # 有參:1.運(yùn)行typeassert(...)返回結(jié)果是decorate,此時(shí)參數(shù)都傳給kwargs 2.People=decorate(People)
class People:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary


p1 = People('nick', 18, 3333.3)
# 類的裝飾器開始運(yùn)行啦------> {'name': <class 'str'>, 'age': <class 'int'>, 'salary': <class 'float'>}

4、描述符與類裝飾器結(jié)合使用

class Typed:
    def __init__(self, name, expected_type):
        self.name = name
        self.expected_type = expected_type

    def __get__(self, instance, owner):
        print('get--->', instance, owner)
        if instance is None:
            return self
        return instance.__dict__[self.name]

    def __set__(self, instance, value):
        print('set--->', instance, value)
        if not isinstance(value, self.expected_type):
            raise TypeError('Expected %s' % str(self.expected_type))
        instance.__dict__[self.name] = value

    def __delete__(self, instance):
        print('delete--->', instance)
        instance.__dict__.pop(self.name)


def typeassert(**kwargs):
   def decorate(cls):
        print('類的裝飾器開始運(yùn)行啦------>', kwargs)
        for name, expected_type in kwargs.items():
            setattr(cls, name, Typed(name, expected_type))
        return cls

    return decorate


@typeassert(name=str, age=int,  salary=float)  # 有參:1.運(yùn)行typeassert(...)返回結(jié)果是decorate,此時(shí)參數(shù)都傳給kwargs 2.People=decorate(People)
class People:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary


print(People.__dict__)
# 類的裝飾器開始運(yùn)行啦------> {'name': <class 'str'>, 'age': <class 'int'>, 'salary': <class 'float'>}
# {'__module__': '__main__', '__init__': , '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None, 'name': <__main__.Typed object at 0x000000000238F8B0>, 'age': <__main__.Typed object at 0x000000000238FF40>, 'salary': <__main__.Typed object at 0x000000000238FFA0>}

p1 = People('nick', 18, 3333.3)


# set---> <__main__.People object at 0x0000000001E07490> nick
# set---> <__main__.People object at 0x0000000001E07490> 18
# set---> <__main__.People object at 0x0000000001E07490> 3333.3

5、利用描述符原理自定制@property

實(shí)現(xiàn)延遲計(jì)算(本質(zhì)就是把一個(gè)函數(shù)屬性利用裝飾器原理做成一個(gè)描述符:類的屬性字典中函數(shù)名為key,value為描述符類產(chǎn)生的對(duì)象)

class Lazyproperty:
    def __init__(self, func):
        self.func = func

    def __get__(self, instance, owner):
        print('這是我們自己定制的靜態(tài)屬性,r1.area實(shí)際是要執(zhí)行r1.area()')
        if instance is None:
            return self
        else:
            print('--->')
            value = self.func(instance)
            setattr(instance, self.func.__name__, value)  # 計(jì)算一次就緩存到實(shí)例的屬性字典中
            return value


class Room:
    def __init__(self, name, width, length):
        self.name = name
        self.width = width
        self.length = length

    @Lazyproperty  # area=Lazyproperty(area) 相當(dāng)于'定義了一個(gè)類屬性,即描述符'
    def area(self):
        return self.width * self.length


r1 = Room('alex', 1, 2)
print(r1.area)  # 先從自己的屬性字典找,沒有再去類的中找,然后出發(fā)了area的__get__方法

# 這是我們自己定制的靜態(tài)屬性,r1.area實(shí)際是要執(zhí)行r1.area()
# --->
# 2

print(r1.area)  # 先從自己的屬性字典找,找到了,是上次計(jì)算的結(jié)果,這樣就不用每執(zhí)行一次都去計(jì)算
# 2

6、自定制@classmethod

class ClassMethod:
    def __init__(self, func):
        self.func = func

    def __get__(self, instance, owner):  # 類來調(diào)用,instance為None,owner為類本身,實(shí)例來調(diào)用,instance為實(shí)例,owner為類本身,
        def feedback(*args, **kwargs):
            print('在這里可以加功能啊...')
            return self.func(owner, *args, **kwargs)

        return feedback


class People:
    name = 'nick'

    @ClassMethod  # say_hi=ClassMethod(say_hi)
    def say_hi(cls, msg):
        print('你好啊,帥哥 %s %s' % (cls.name, msg))


People.say_hi('你是那偷心的賊')

p1 = People()
# 在這里可以加功能啊...
# 你好啊,帥哥 nick 你是那偷心的賊

p1.say_hi('你是那偷心的賊')
# 在這里可以加功能啊...
# 你好啊,帥哥 nick 你是那偷心的賊

7、自定制@staticmethod

class StaticMethod:
    def __init__(self, func):
        self.func = func

    def __get__(self, instance, owner):  # 類來調(diào)用,instance為None,owner為類本身,實(shí)例來調(diào)用,instance為實(shí)例,owner為類本身
        def feedback(*args, **kwargs):
            print('在這里可以加功能啊...')
            return self.func(*args, **kwargs)

        return feedback


class People:
    @StaticMethod  # say_hi = StaticMethod(say_hi)
    def say_hi(x, y, z):
        print('------>', x, y, z)


People.say_hi(1, 2, 3)
# 在這里可以加功能啊...
# ------> 1 2 3

p1 = People()
p1.say_hi(4, 5, 6)
# 在這里可以加功能啊...
# ------> 4 5 6

十五、元類(metaclass)

元類:負(fù)責(zé)產(chǎn)生該對(duì)象的類稱之為元類,即元類可以簡稱為類的類

用class關(guān)鍵字創(chuàng)建一個(gè)類,用的默認(rèn)的元類type,因此以前說不要用type作為類別判斷

class People:  # People=type(...)
    country = 'China'

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def eat(self):
        print('%s is eating' % self.name)

print(type(People))
<class 'type'>

1、type實(shí)現(xiàn)

  • 創(chuàng)建類的3個(gè)要素:類名,基類,類的名稱空間
  • People = type(類名,基類,類的名稱空間)
class_name = 'People'  # 類名
class_bases = (object,)  # 基類

class_dic = {}  # 類的名稱空間
class_body = """
country='China'
def __init__(self,name,age):
    self.name=name
    self.age=age
def eat(self):
    print('%s is eating' %self.name)
"""

exec(class_body, {}, class_dic, )  #執(zhí)行class_body中的代碼,然后把產(chǎn)生的名字丟入class_dic字典中

print(class_name)  # People
print(class_bases)  # (<class 'object'>,)
print(class_dic)  # 類的名稱空間
# {'country': 'China', '__init__': , 'eat': }


People1 = type(class_name, class_bases, class_dic)
print(People1)  # <class '__main__.People'>

obj1 = People1(1, 2)
obj1.eat()  # 1 is eating

2、自定義元類控制類

自定義元類控制類的產(chǎn)生過程,類的產(chǎn)生過程其實(shí)就是元類的調(diào)用過程。

分析用class自定義類的運(yùn)行原理(而非元類的的運(yùn)行原理):

  • 拿到一個(gè)字符串格式的類名class_name='People'

  • 拿到一個(gè)類的基類們class_bases=(obejct,)

  • 執(zhí)行類體代碼,拿到一個(gè)類的名稱空間class_dic={...}

  • 調(diào)用People=type(class_name,class_bases,class_dic)

class Mymeta(type):  # 只有繼承了type類才能稱之為一個(gè)元類,否則就是一個(gè)普通的自定義類
    def __init__(self, class_name, class_bases, class_dic):
        print('self:', self)  # 現(xiàn)在是People
        print('class_name:', class_name)
        print('class_bases:', class_bases)
        print('class_dic:', class_dic)
        super(Mymeta, self).__init__(class_name, class_bases, class_dic)  # 重用父類type的功能


class People(object, metaclass=Mymeta):  # People=Mymeta(類名,基類們,類的名稱空間)
    country = 'China'

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def eat(self):
        print('%s is eating' % self.name)

# self: <class '__main__.People'>
# class_name: People
# class_bases: (<class 'object'>,)
# class_dic: {'__module__': '__main__', '__qualname__': 'People', 'country': 'China', '__init__': , 'eat': }

應(yīng)用:我們可以控制類必須有文檔。

class Mymeta(type):  # 只有繼承了type類才能稱之為一個(gè)元類,否則就是一個(gè)普通的自定義類
    def __init__(self, class_name, class_bases, class_dic):
        if class_dic.get('__doc__') is None or len(
                class_dic.get('__doc__').strip()) == 0:
            raise TypeError('類中必須有文檔注釋,并且文檔注釋不能為空')
        if not class_name.istitle():
            raise TypeError('類名首字母必須大寫')
        super(Mymeta, self).__init__(class_name, class_bases,   class_dic)  # 重用父類的功能


try:

    class People(object, metaclass=Mymeta ):  # People  = Mymeta('People',(object,),{....})
        #     """這是People類"""
        country = 'China'

        def __init__(self, name, age):
            self.name = name
            self.age = age

        def eat(self):
            print('%s is eating' % self.name)
except Exception as e:
    print(e)
# 類中必須有文檔注釋,并且文檔注釋不能為空

3、自定義元類控制類的實(shí)例化

類的調(diào)用,即類實(shí)例化就是元類的調(diào)用過程,可以通過元類Mymeta的__call__方法控制。

繼承的查找順序:子類->Class –>object–> Mymeta->type

class Mymeta(type):
    def __call__(self, *args, **kwargs):
        print(self)  # self是People
        print(args)  # args = ('nick',)
        print(kwargs)  # kwargs = {'age':18}
        # return 123
        # 1. 先造出一個(gè)People的空對(duì)象,申請(qǐng)內(nèi)存空間
        # __new__方法接受的參數(shù)雖然也是和__init__一樣,但__init__是在類實(shí)例創(chuàng)建之后調(diào)用,而 __new__方法正是創(chuàng)建這個(gè)類實(shí)例的方法。
        obj = self.__new__(self)  # 雖然和下面同樣是People,但是People沒有,找到的__new__是父類的
        # 2. 為該對(duì)空對(duì)象初始化獨(dú)有的屬性
        self.__init__(obj, *args, **kwargs)
        # 3. 返回一個(gè)初始化好的對(duì)象
        return obj


class People(object, metaclass=Mymeta):  # People = Mymeta(),People()則會(huì)觸發(fā)__call__
    country = 'China'

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def eat(self):
        print('%s is eating' % self.name)


#     在調(diào)用Mymeta的__call__的時(shí)候,首先會(huì)找自己(如下函數(shù))的,自己的沒有才會(huì)找父類的
#     def __new__(cls, *args, **kwargs):
#         # print(cls)  # cls是People
#         # cls.__new__(cls) # 錯(cuò)誤,無限死循環(huán),自己找自己的,會(huì)無限遞歸
#         obj = super(People, cls).__new__(cls)  # 使用父類的,則是去父類中找__new__
#         return obj

obj = People('nick', age=18)
# <class '__main__.People'>
# ('nick',)
# {'age': 18}

print(obj.__dict__)
# {'name': 'nick', 'age': 18}

4、練習(xí):使用元類修改屬性為隱藏屬性

class Mymeta(type):
    def __init__(self, class_name, class_bases, class_dic):
        # 加上邏輯,控制類Foo的創(chuàng)建
        super(Mymeta, self).__init__(class_name, class_bases, class_dic)

    def __call__(self, *args, **kwargs):
        # 加上邏輯,控制Foo的調(diào)用過程,即Foo對(duì)象的產(chǎn)生過程
        obj = self.__new__(self)
        self.__init__(obj, *args, **kwargs)
        # 修改屬性為隱藏屬性
        obj.__dict__ = {
            '_%s__%s' % (self.__name__, k): v
            for k, v in obj.__dict__.items()
        }

        return obj


class Foo(object, metaclass=Mymeta):  # Foo = Mymeta(...)
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex


obj = Foo('nick', 18, 'male')
print(obj.age)  # 'Foo' object has no attribute 'age'
print(obj.__dict__)
# {'_Foo__name': 'egon', '_Foo__age': 18, '_Foo__sex': 'male'}

5、利用元類實(shí)現(xiàn)單例模式

NAME = 'nick'
AGE = 18

class Mymeta(type):
    def __init__(self,class_name,class_bases,class_dict):
        super().__init__(class_name,class_bases,class_dict)
        self.__instance = self(NAME,AGE)
     
    def __call__(self,*args,**kwargs):
        
        if len(args) == 0 and len(kwargs) == 0:
return self.__instance
        
        obj = object.__new__(self)
        self.__init__(obj,*args,**kwargs)
        
        return obj
    
class People(metaclass=Mymeta):
    def __init__(self,name,age):
        self.name = name
        self.age = age
        
peo1 = People()
peo2 = People()

到此這篇關(guān)于Python面向?qū)ο缶幊痰奈恼戮徒榻B到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)簡單的HttpServer服務(wù)器示例

    Python實(shí)現(xiàn)簡單的HttpServer服務(wù)器示例

    本篇文章主要介紹了Python實(shí)現(xiàn)簡單的HttpServer服務(wù)器示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • python神經(jīng)網(wǎng)絡(luò)slim常用函數(shù)訓(xùn)練保存模型

    python神經(jīng)網(wǎng)絡(luò)slim常用函數(shù)訓(xùn)練保存模型

    這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)使用slim函數(shù)進(jìn)行模型的訓(xùn)練及保存模型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python使用在線API查詢IP對(duì)應(yīng)的地理位置信息實(shí)例

    python使用在線API查詢IP對(duì)應(yīng)的地理位置信息實(shí)例

    這篇文章主要介紹了python使用在線API查詢IP對(duì)應(yīng)的地理位置信息實(shí)例,需要的朋友可以參考下
    2014-06-06
  • Python 實(shí)現(xiàn)繪制子圖及子圖刻度的變換等問題

    Python 實(shí)現(xiàn)繪制子圖及子圖刻度的變換等問題

    這篇文章主要介紹了Python 實(shí)現(xiàn)繪制子圖及子圖刻度的變換等問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 關(guān)于Word2Vec可視化展示

    關(guān)于Word2Vec可視化展示

    這篇文章主要介紹了關(guān)于Word2Vec可視化展示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python連接字符串過程詳解

    Python連接字符串過程詳解

    這篇文章主要介紹了python連接字符串過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Python求一批字符串的最長公共前綴算法示例

    Python求一批字符串的最長公共前綴算法示例

    這篇文章主要介紹了Python求一批字符串的最長公共前綴算法,涉及Python針對(duì)字符串的遍歷、判斷、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Anaconda的安裝及其環(huán)境變量的配置詳解

    Anaconda的安裝及其環(huán)境變量的配置詳解

    這篇文章主要介紹了Anaconda的安裝及其環(huán)境變量的配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • matplotlib相關(guān)系統(tǒng)目錄獲取方式小結(jié)

    matplotlib相關(guān)系統(tǒng)目錄獲取方式小結(jié)

    這篇文章主要介紹了matplotlib相關(guān)系統(tǒng)目錄獲取方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Python批量裁剪圖形外圍空白區(qū)域

    Python批量裁剪圖形外圍空白區(qū)域

    這篇文章主要介紹了Python批量裁剪圖形外圍空白區(qū)域,批量裁剪掉圖片的背景區(qū)域,一般是白色背景,從而減少背景值的干擾和減少存儲(chǔ),下面文章的具體操作內(nèi)容需要的小伙伴可以參考一下
    2022-04-04

最新評(píng)論