面向对象高阶-03实现点方法的魔法方法及原理

实现点方法的魔法方法:

  • getattr
  • setattr
  • delattr
  • getattribut

getattr

  • 只有在使用点调用属性且属性不存在的时候才会触发
f1.xxxxxx

输出结果:
----> from getattr:你找的属性不存在

setattr

  • 添加/修改属性会触发它的执行
print(f1.__dict__
      )  # 因为你重写了__setattr__,凡是赋值操作都会触发它的运行,你啥都没写,就是根本没赋值,除非你直接操作属性字典,否则永远无法赋值
f1.z = 3
print(f1.__dict__)

输出结果:
会多一个 key为z value为3 的键

delattr

  • 删除属性的时候会触发
f1.__dict__['a'] = 3  # 我们可以直接修改属性字典,来完成添加/修改属性的操作
del f1.a
print(f1.__dict__)

输出结果
----> from delattr

getattribute

用来获取属性,优先级高
在获取属性时如果存在getattribute则先执行该函数,如果没有拿到属性则继续调用 getattr函数,如果拿到了则直接返回
当__getattribute__与__getattr__同时存在,只会执行__getattrbute__,除非__getattribute__在执行过程中抛出异常AttributeError!!!

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__
1

class A:

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

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

a = A()
print(a.name)

输出结果:
__getattribute__
None

两次不同原因!!

这边是因为第一次调用了父类的__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
posted @ 2019-09-21 10:56  suren_apan  阅读(91)  评论(0编辑  收藏  举报