面向对象高阶-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
相关文章
相关标签/搜索