__getattr__() 和 __getattribute__()
来源:互联网 发布:闻rush什么感觉 知乎 编辑:程序博客网 时间:2024/06/05 01:04
转自:http://my.oschina.net/letiantian/blog/175578
关于__getattr__
object.__getattr__(self, name) 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). name is the attribute name. This method should return the (computed) attribute value or raise an AttributeError exception. Note that if the attribute is found through the normal mechanism, __getattr__() is not called. (This is an intentional asymmetry between __getattr__() and __setattr__().) This is done both for efficiency reasons and because otherwise __getattr__() would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the __getattribute__() method below for a way to actually get total control in new-style classes.
关于__get__getattribute__
object.__getattribute__(self, name)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. This method should return the (computed) attribute value or raise an AttributeError exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, object.__getattribute__(self, name).Note This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions.
例子:
__getattr__示例:class Test(object): def __init__(self,name): self.name = name def __getattr__(self, value): if value == 'address': return 'China'if __name__=="__main__": test = Test('letian') print test.name print test.address test.address = 'Anhui' print test.address
运行结果: letianChinaAnhui
如果是调用了一个类中未定义的方法,则__getattr__也要返回一个方法,例如: class Test(object): def __init__(self,name): self.name = name def __getattr__(self, value): return lenif __name__=="__main__": test = Test('letian') print test.getlength('letian')
运行结果: 6
__getattribute__示例: class Test(object): def __init__(self,name): self.name = name def __getattribute__(self, value): if value == 'address': return 'China' if __name__=="__main__": test = Test('letian') print test.name print test.address test.address = 'Anhui' print test.address
运行结果: NoneChinaChina
参考:
Data model: http://docs.python.org/2/reference/datamodel.htmlOverwriting __getattr__ makes help(…) fail with TypeError:http://stackoverflow.com/questions/5157746/overwriting-getattr-makes-help-fail-with-typeerror
0 0
- python:__getattr__() 和 __getattribute__()
- __getattr__() 和 __getattribute__()
- python __getattr__和__getattribute__
- __getattr__和__getattribute__的区别
- python(第五天):__getattr__ 和 __getattribute__
- python __getattribute__ 和 __getattr__的区别
- __getattr__ VS __getattribute__
- python __get__ __getattr__ __getattribute__
- Python __getattr__ __getattribute__
- Python __getattr__ __getattribute__ __setattr__
- Python----__getattribute__, __getattr__
- 详解Python中 __get__和__getattr__和__getattribute__的区别
- python中__get__, __getattr__和__getattribute__区别说明
- python中__getattr__()和__getattribute__()方法的区别
- python __setattr__, __getattr__, __delattr__,__getattribute__
- Python笔记:getattr,__getattr__,__getattribute__
- Python __getattribute__ vs __getattr__ 浅谈
- Python __getattribute__、__getattr__、__get__总结
- 欢迎使用CSDN-markdown编辑器
- hdu 4759 Poker Shuffle 进制(找规律)★
- 使用Ubuntu在局域网中共享文件
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
- ip到浏览器的网络知识
- __getattr__() 和 __getattribute__()
- NSFileManager的使用保存图片,并进行增删查改,还进入部分插入
- [初级知识]Linux内核开发中的同步问题
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- 一个c语言的函数执行
- Zend Studio使用教程之自定义并注册Zend Studio(2/2)
- navicatPremium 连接postgresql 教程
- AngularJS
- 利用poi实现table表格导出excel