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

Python面向對象編程(三)

 更新時間:2022年05月30日 09:08:14   作者:springsnow  
本文詳細講解了Python的面向對象編程,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Python面向對象編程(一)

Python面向對象編程(二)

Python面向對象編程(三)

一、isinstance和issubclass

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


class Bar(Foo):
    pass


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

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

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


print(issubclass(Bar, Foo))
# True

print(issubclass(Foo, object))
# True

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

1、反射在類中的使用

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

  • hasattr:判斷一個方法是否存在與這個類中
  • getattr:根據(jù)字符串去獲取obj對象里的對應的方法的內存地址,加"()"括號即可執(zhí)行
  • setattr:通過setattr將外部的一個函數(shù)綁定到實例中
  • delattr:刪除一個實例或者類中的方法
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、反射在模塊中的使用

動態(tài)導入一個模塊__import__,并且動態(tài)輸入函數(shù)名然后執(zhí)行相應功能。

注意:getattr,hasattr,setattr,delattr對模塊的修改都在內存中進行,并不會影響文件中真實內容。

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

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

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

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

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

3、實例:基于反射機制模擬web框架路由

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

# 動態(tài)導入模塊,并執(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最后一個字符串
if hasattr(m, inp):  # 判斷在commons模塊中是否存在inp這個字符串
    inp= getattr(m, inp)  # 獲取inp的引用
    inp()  # 執(zhí)行
else:
    print("404")

三、__getattr__、__setattr__和__delattr__和__getattribute__事件

  • __getattr__:只有在使用點調用屬性且屬性不存在的時候才會觸發(fā)。比較有用
  • __delattr__:刪除屬性的時候會觸發(fā)
  • __setattr__:添加/修改屬性會觸發(fā)它的執(zhí)行
    當你自己寫__getattr__、__delattr__、__setattr__方法,系統(tǒng)會調用你寫的方法,如果沒寫,系統(tǒng)調用默認
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  # 應該使用它

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


f1 = Foo(10)
# ----> from setattr
print(f1.__dict__ )  # 因為你重寫了__setattr__,凡是賦值操作都會觸發(fā)它的運行,你啥都沒寫,就是根本沒賦值。除非你直接操作屬性字典,否則永遠無法賦值
# {}

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

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

__getattribute__

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

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

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


f1 = Foo(10)

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

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

當__getattribute__與__getattr__同時存在,只會執(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('不管是否存在,我都會執(zhí)行')
        raise AttributeError('哈哈')


f1 = Foo(10)

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

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

四、__setitem__和__getitem和__delitem__

  • __setitem__:中括號賦值時觸發(fā)
  • __getitem__:中括號取值時觸發(fā)
  • __delitem__:中括號刪除時觸發(fā)
  • __delattr__:.刪除時觸發(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]時,delitem執(zhí)行')
        self.__dict__.pop(key)

    def __delattr__(self, item):
        print('del obj.key時,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時,delattr執(zhí)行

del f1['age']
# del obj[key]時,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):
        # 默認打印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__:析構方法

會在對象被刪除之前自動觸發(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)資源相關的事情
        self.f.close()


obj = People('egon', 18)

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

七、__slots__

使用點來訪問屬性本質就是在訪問類或者對象的__dict__屬性字典(類的字典是共享的,而每個實例的是獨立的)。

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

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

class Foo:
    __slots__ = 'x'


f1 = Foo()
f1.x = 1
f1.y = 2  # 報錯
print(f1.__slots__ )  # f1不再有__dict__

當你定義__slots__后,__slots__就會為實例使用一種更加緊湊的內部表示。使用__slots__后不能再給實例添加新的屬性了,只能使用在__slots__中定義的那些屬性名。

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

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

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


n = Bar()
n.x, n.y = 1, 2
n.z = 3  # 報錯

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

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


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

該屬性無法被繼承

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


class Bar(Foo):
    pass


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

九、__call__:會在調用對象時自動觸發(fā)。

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

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__:類構造器

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

__new__方法(第一個執(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__方法,但在當繼承一個不可變的類型(例如str類,int類等)時,它的特性就尤顯重要了。我們舉下面這個例子:

# 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):
        """對象實例化的時候自動觸發(fā)"""
        self.name = name
        self.age = age

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


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

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

  • 如果__str__沒有被定義,那么就會使用__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__ 表示當前操作的對象在那個模塊
  • __class__表示當前操作的對象的類是什么
# 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,即:輸出類

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

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

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

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

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

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

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('with中代碼塊執(zhí)行完畢時執(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語句,對象的__enter__被觸發(fā),有返回值則賦值給as聲明的變量
# =====>執(zhí)行代碼塊
# with中代碼塊執(zhí)行完畢時執(zhí)行我啊
# <class 'AttributeError'>
# ***著火啦,救火啊***
#
# ***著火啦,救火啊***

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

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

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

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('with中代碼塊執(zhí)行完畢時執(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)  # ------------------------------->會執(zhí)行

# 出現(xiàn)with語句,對象的__enter__被觸發(fā),有返回值則賦值給as聲明的變量
# =====>執(zhí)行代碼塊
# with中代碼塊執(zhí)行完畢時執(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__)

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

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

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

  • __get__():調用一個屬性時,觸發(fā)
  • __set__():為一個屬性賦值時,觸發(fā)
  • __delete__():采用del刪除屬性時,觸發(fā)

描述符的作用是用來代理另外一個類的屬性的。包含這三個方法的新式類稱為描述符,由這個類產生的實例進行屬性的調用/賦值/刪除,并不會觸發(fā)這三個方法

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
#無任何輸出結果?。。?/pre>

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

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

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

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

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


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

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

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

    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設置...
# Int設置...

p1.name  # Str調用
p1.name = 'nick'  # Str設置...
del p1.name  # Str刪除...

p1.age  # Int調用
p1.age = 18  # Int設置...
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ù)的賦值沒有類型限制,下面我們通過描述符機制來實現(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('類的裝飾器開始運行啦------>')
    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)

# 類的裝飾器開始運行啦------>

3、類的裝飾器:有參

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

    return decorate


@typeassert( name=str, age=int, salary=float)  # 有參:1.運行typeassert(...)返回結果是decorate,此時參數(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)
# 類的裝飾器開始運行啦------> {'name': <class 'str'>, 'age': <class 'int'>, 'salary': <class 'float'>}

4、描述符與類裝飾器結合使用

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('類的裝飾器開始運行啦------>', 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.運行typeassert(...)返回結果是decorate,此時參數(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__)
# 類的裝飾器開始運行啦------> {'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

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

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

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


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

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


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

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

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

6、自定制@classmethod

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

    def __get__(self, instance, owner):  # 類來調用,instance為None,owner為類本身,實例來調用,instance為實例,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):  # 類來調用,instance為None,owner為類本身,實例來調用,instance為實例,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)

元類:負責產生該對象的類稱之為元類,即元類可以簡稱為類的類

用class關鍵字創(chuàng)建一個類,用的默認的元類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實現(xiàn)

  • 創(chuàng)建類的3個要素:類名,基類,類的名稱空間
  • 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中的代碼,然后把產生的名字丟入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、自定義元類控制類

自定義元類控制類的產生過程,類的產生過程其實就是元類的調用過程。

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

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

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

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

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

class Mymeta(type):  # 只有繼承了type類才能稱之為一個元類,否則就是一個普通的自定義類
    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': }

應用:我們可以控制類必須有文檔。

class Mymeta(type):  # 只有繼承了type類才能稱之為一個元類,否則就是一個普通的自定義類
    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、自定義元類控制類的實例化

類的調用,即類實例化就是元類的調用過程,可以通過元類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. 先造出一個People的空對象,申請內存空間
        # __new__方法接受的參數(shù)雖然也是和__init__一樣,但__init__是在類實例創(chuàng)建之后調用,而 __new__方法正是創(chuàng)建這個類實例的方法。
        obj = self.__new__(self)  # 雖然和下面同樣是People,但是People沒有,找到的__new__是父類的
        # 2. 為該對空對象初始化獨有的屬性
        self.__init__(obj, *args, **kwargs)
        # 3. 返回一個初始化好的對象
        return obj


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

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

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


#     在調用Mymeta的__call__的時候,首先會找自己(如下函數(shù))的,自己的沒有才會找父類的
#     def __new__(cls, *args, **kwargs):
#         # print(cls)  # cls是People
#         # cls.__new__(cls) # 錯誤,無限死循環(huán),自己找自己的,會無限遞歸
#         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、練習:使用元類修改屬性為隱藏屬性

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的調用過程,即Foo對象的產生過程
        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、利用元類實現(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()

到此這篇關于Python面向對象編程的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

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

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

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

    python神經網絡slim常用函數(shù)訓練保存模型

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

    python使用在線API查詢IP對應的地理位置信息實例

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

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

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

    關于Word2Vec可視化展示

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

    Python連接字符串過程詳解

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

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

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

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

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

    matplotlib相關系統(tǒng)目錄獲取方式小結

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

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

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

最新評論