Python.__getattr__Vs__getattribute__
__getattr__ Vs __getattribute__
class Fish(object):
def __getattr__(self, key):
if key == 'color':
print 'access color'
return 'blue'
else:
raise AttributeError
def __getattribute__(self, key):
print '__getattribute__ %s' %key
return object.__getattribute__(self, key)
if __name__ == "__main__":
f = Fish()
print "First %s \n" %f.color
print '-----------------------\n'
print "Second %s" %f.color
以上代码的输出如下:
“
__getattribute__ color
access color
First blue
-----------------------
__getattribute__ color
access color
Second blue
”
http://docs.python.org/2/reference/datamodel.html
3.4.2. Customizing attribute access
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 anAttributeError 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.
3.4.2.1. More attribute access for new-style classes
The following methods only apply to new-style classes.
- 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. See Special method lookup for new-style classes.
Python.__getattr__Vs__getattribute__的更多相关文章
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python 小而美的函数
python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况 any any(iterable) ...
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- 使用Python保存屏幕截图(不使用PIL)
起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...
- Python编码记录
字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...
- Apache执行Python脚本
由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...
- python开发编译器
引言 最近刚刚用python写完了一个解析protobuf文件的简单编译器,深感ply实现词法分析和语法分析的简洁方便.乘着余热未过,头脑清醒,记下一点总结和心得,方便各位pythoner参考使用. ...
随机推荐
- FBV和CBV区别
FBV和CBV区别 def dispatch(self, request, *args, **kwargs): # 做分发的 if request.meth ...
- 代码生成器 CodeSmith 的使用(一)
由于在项目中经常要会用到数据库的 CRUD 操作(增.删.改.查),而且还使用的是orm 框架将数据库表名和表中的的字段映射成相应的类属性.如果把大量的时间用到手工输入数据库表中的字段,除了能练习打字 ...
- Mysql-两表的连接,copy表,select的各种用法
-- 连接:外连接,内连接 两个表之间 外连接:right join left join -- left join 左标为主 一般以值少的为主 select * from table1 left ...
- Openstack虚机实例状态错误手工恢复vm_state:error
Openstack虚机实例状态错误手工恢复vm_state:error 1.找到状态为出错状态的VM.在数据库里面表现Status为ERROR而非ACTIVE. 2.找到出错状态VM的UUID. 3. ...
- 关于putty连接百度云linux服务器那些事
看有活动,30元半年的百度云服务器,就直接买了当练手的玩 买完之后,发现使用putty不能直接连接百度云的centos服务器, 用了putty和ssh都不能连接 试了好几次,都打算尝试用秘钥对的形式了 ...
- php 面试考点总结-高并发和大流量解决方案考点
1.web资源防盗链 盗链概念 盗链是指在自己的页面上展示一些并不在自己服务器的内容 防盗链工作原理 通过referer或者签名,网站可以检测目标网页访问的来源页,一旦检测到来源页不是本站即进行阻止或 ...
- delphi XE7 判断手机返回键
Using the Android Device's Back Button To make your application handle when users press the Back but ...
- Eclipse 工程使用相对路径导入Jar包设置
环境:MyEclipse 6.5 问题:MyEclipse 工程使用相对路径导入Jar包 我们在导入工程时,往往添加Jar都是使用的绝对路径,但这带来了一个问题,不同的用户使用工程都得重新配置Buil ...
- 迷你MVVM框架 avalonjs 学习教程16、过滤器
avalon的过滤器是参考自angular与rivets.它也被称做管道文本过滤器,它的处理对象只能是文本(字符串),它只能用在文本绑定中,并且只能是双花括号形式.下面是各大家的过滤器比较: rive ...
- virtual abstract override new 几点学习
1.Vitual方法和普通方法区别为:继承其的子类可以用override/new在重载此方法,也可以不重载其方法,有方法体(可以写语句),override修饰则调用子类方法2.abstract类中抽象 ...