博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python:__getattr__() 和 __getattribute__()
阅读量:6287 次
发布时间:2019-06-22

本文共 2763 字,大约阅读时间需要 9 分钟。

hot3.png

关于__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:
Overwriting __getattr__ makes help(…) fail with TypeError:

转载于:https://my.oschina.net/letiantian/blog/175578

你可能感兴趣的文章
iOS 多线程总结
查看>>
webpack是如何实现前端模块化的
查看>>
TCP的三次握手四次挥手
查看>>
关于redis的几件小事(六)redis的持久化
查看>>
webpack4+babel7+eslint+editorconfig+react-hot-loader 搭建react开发环境
查看>>
Maven 插件
查看>>
初探Angular6.x---进入用户编辑模块
查看>>
计算机基础知识复习
查看>>
【前端词典】实现 Canvas 下雪背景引发的性能思考
查看>>
大佬是怎么思考设计MySQL优化方案的?
查看>>
<三体> 给岁月以文明, 给时光以生命
查看>>
Android开发 - 掌握ConstraintLayout(九)分组(Group)
查看>>
springboot+logback日志异步数据库
查看>>
Typescript教程之函数
查看>>
Android 高效安全加载图片
查看>>
vue中数组变动不被监测问题
查看>>
3.31
查看>>
类对象定义 二
查看>>
收费视频网站Netflix:用户到底想要“点”什么?
查看>>
MacOS High Sierra 12 13系统转dmg格式
查看>>