面向對象高階-03實現點方法的魔法方法及原理

實現點方法的魔法方法:python

  • getattr
  • setattr
  • delattr
  • getattribut

getattr

  • 只有在使用點調用屬性且屬性不存在的時候纔會觸發
f1.xxxxxx

輸出結果:
----> from getattr:你找的屬性不存在函數

setattr

  • 添加/修改屬性會觸發它的執行
print(f1.__dict__
      )  # 由於你重寫了__setattr__,凡是賦值操做都會觸發它的運行,你啥都沒寫,就是根本沒賦值,除非你直接操做屬性字典,不然永遠沒法賦值
f1.z = 3
print(f1.__dict__)

輸出結果:
會多一個 key爲z value爲3 的鍵code

delattr

  • 刪除屬性的時候會觸發
f1.__dict__['a'] = 3  # 咱們能夠直接修改屬性字典,來完成添加/修改屬性的操做
del f1.a
print(f1.__dict__)

輸出結果
----> from delattr遞歸

getattribute

用來獲取屬性,優先級高
在獲取屬性時若是存在getattribute則先執行該函數,若是沒有拿到屬性則繼續調用 getattr函數,若是拿到了則直接返回
當__getattribute__與__getattr__同時存在,只會執行__getattrbute__,除非__getattribute__在執行過程當中拋出異常AttributeError!!!get

class A:

    def __getattr__(self, item):
        print("__getattr__")
        return 1

    def __getattribute__(self, item):
        print("__getattribute__")
        # 用下面這個會無限遞歸
        # return self.__dict__[item]
        return super().__getattribute__(item)

a = A()
print(a.name)

輸出結果:
__getattribute__
__getattr__
1it

class A:

    def __getattr__(self, item):
        print("__getattr__")
        return 1

    def __getattribute__(self, item):
        print("__getattribute__")

a = A()
print(a.name)

輸出結果:
__getattribute__
Noneclass

兩次不一樣緣由!!

這邊是由於第一次調用了父類的__getattribute__,由於不存在,因此調用了__getattr__
第二次默認返回None,由於程序也直接結束,打印了最後返回值->None程序

多魔法方法結合例子:

列子1方法

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  # 這就無限遞歸了,由於這邊賦值會無限調用__setattr__ 方法
        # self.__dict__[key] = value  # 應該使用它

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

f1 = Foo(10)

列子2異常

class A:
    def __setattr__(self, key, value):
        print(key)
        print(value)
        print("__setattr__")
        self.__dict__[key] = value

    def __delattr__(self, item):
        print("__delattr__")
        print(item)
        self.__dict__.pop(item)
        
    def __getattr__(self, item):
        print("__getattr__")
        return 1

    def __getattribute__(self, item):
        print("__getattribute__")
        # return self.__dict__[item]
        return super().__getattribute__(item)


a = A()
# a.name = "jack"
# # print(a.name)
#
# # del a.name
# print(a.name)
# print(a.xxx)
# a.name = "xxx"
print(a.name)

# b =A()
# b.__dict__["name"] = "jack"
# print(b.name)

列子三-->解:

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)

#__setattr__添加/修改屬性會觸發它的執行
f1=Foo(10)
print(f1.__dict__) # 由於你重寫了__setattr__,凡是賦值操做都會觸發它的運行,你啥都沒寫,就是根本沒賦值,除非你直接操做屬性字典,不然永遠沒法賦值
f1.z=3
print(f1.__dict__)

#__delattr__刪除屬性的時候會觸發
f1.__dict__['a']=3#咱們能夠直接修改屬性字典,來完成添加/修改屬性的操做
del f1.a
print(f1.__dict__)

#__getattr__只有在使用點調用屬性且屬性不存在的時候纔會觸發
f1.xxxxxx
相關文章
相關標籤/搜索