python __setattr__, __getattr__, __delattr__,__getattribute__

参考资料

  

  

  

__setattr__、__getattr__和__delattr__以及__getattribute__可以拦截对对象属性的访问;

  

  

>>> s = Something()

>>> s.age = 3

set 'age' = 3

>>> s.age

3

注意到,s.age并没有调用__getattr__,是因为__getattr__方法只在属性没有找到的时候调用

  

虽然相比于Property,实现优点复杂(可能效率更低?),但是足够灵活,功能强大;

  

另外,对于上述代码,访问未绑定的属性,将引发异常

>>> s.name

get 'name'

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "<stdin>", line 4, in __getattr__

KeyError: 'name'

注意:因为name没有绑定到s,所以__getattr__被调用,而用不存在的key访问dictionary会引发KeyError异常;

  

更严重的是,改变了hasattr的行为

>>> hasattr(s,'name')

get 'name'

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "<stdin>", line 4, in __getattr__

KeyError: 'name'

  

  

需要注意的是,防止循环调用,所以__dict__被用用来代替普通的特性赋值操作;另外,__getattribute__拦截所有属性的访问(在新式类中),也拦截对__dict__的访问!访问__getattribute__中与self相关的属性时,使用超类的__getattribute__方法(使用super函数)是唯一安全的途径;

  

>>> s = Something()

>>> s.name

get name

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "<stdin>", line 4, in __getattribute__

AttributeError: 'Something' object has no attribute 'name'

>>> s.name = 3

>>> s.name

get name

3

  

__getattribute__每次都被调用,不管属性是否被绑定;

  

__getattribute__没有__getattr__的异常问题:

>>> hasattr(s,'t')

False

  

使用__getattribute__的时候,要小心,因为它拦截所有的属性访问,包括__dict__,所以

def __getattribute__(self, key):

    …

    return self.__dict__[key]

会发生循环调用,使用super对象调用__getattribute__可以避免;

  

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值