python(第五天):__getattr__ 和 __getattribute__
来源:互联网 发布:淘宝商品侵权如何处理 编辑:程序博客网 时间:2024/05/21 09:02
This chapter I plan to write by English. 如果有什么错误欢迎指正.
I will talk about the difference between __getattr__(self,name) and __getattribute__(self,name)
Firstly look at the description of __getattr__:
Called when an attribute lookup has not found the attribute in the usual places(i.e. it is not an instance attribute nor is it found in the class tree for self).
arguments:name is the attribute name
This method should return the (computed) attribute value or raise an AttributeErrorexception.
就是说如果在实例的搜索树中没有找到这个属性就调用__getattr__(self,name)
这里要注意一点私有成员的实现方式是_ClassName__attributeName所以当调用__attributeName时也会触发__getattri__.
look at the description of __getattribute__:
Called unconditionally to implement attribute accesses for instances of the class. If the class also defines __getattr__(),the latter will not be called unless __getattribute__() either calls it explicitly or raises an AttributeError.
无条件被调用,通过实例访问属性。如果class中定义了__getattr__(),则__getattr__()不会被调用(除非显示调用或引发AttributeError异常)
def __getattribute__(self, *args, **kwargs): print("__getattribute__() is called") # return object.__getattribute__(self, *args, **kwargs) raise AttributeError def __getattr__(self, name): print("__getattr__() is called ") return name + " from getattr"
如果在上面的代码中 没有 raise AttributeError则不会触发__getattr__
- python(第五天):__getattr__ 和 __getattribute__
- python:__getattr__() 和 __getattribute__()
- python __getattr__和__getattribute__
- python __getattribute__ 和 __getattr__的区别
- python __get__ __getattr__ __getattribute__
- Python __getattr__ __getattribute__
- Python __getattr__ __getattribute__ __setattr__
- Python----__getattribute__, __getattr__
- __getattr__() 和 __getattribute__()
- 详解Python中 __get__和__getattr__和__getattribute__的区别
- python __setattr__, __getattr__, __delattr__,__getattribute__
- Python笔记:getattr,__getattr__,__getattribute__
- Python __getattribute__ vs __getattr__ 浅谈
- Python __getattribute__、__getattr__、__get__总结
- python中__get__, __getattr__和__getattribute__区别说明
- python中__getattr__()和__getattribute__()方法的区别
- __getattr__和__getattribute__的区别
- python中__get__,__getattr__,__getattribute__的区别
- 陈怡暖:2015.5.19早间最强现货黄金白银操作建议
- multiple-media streaming on p2p overlay across oversea network
- 空密码访问共享
- Android开发之时间刻度盘
- 利用openssl实现字符串加密解密
- python(第五天):__getattr__ 和 __getattribute__
- java 虚拟机 垃圾回收器原理和使用总结
- const 和 #define区别
- 一步一步学会http获取tomcat服务端的图片,在android客户端显示
- js 直接添加属性
- Ubuntu 完全卸载Apache2
- IO 异常读请求分析
- weblogic突然挂掉后nodemanager起不来
- [Egret学习笔记 八]TypeScript与JavaScript通讯