__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__(selfname)

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__(selfname)

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__的更多相关文章

  1. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  2. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  3. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  4. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  5. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  6. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  7. Python编码记录

    字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...

  8. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

  9. python开发编译器

    引言 最近刚刚用python写完了一个解析protobuf文件的简单编译器,深感ply实现词法分析和语法分析的简洁方便.乘着余热未过,头脑清醒,记下一点总结和心得,方便各位pythoner参考使用. ...

随机推荐

  1. Executor框架(七)Future 接口、FutureTask类

    Future接口介绍   Future 表示异步计算的结果.它提供了检查计算是否完成的方法,以等待计算的完成,并获取计算的结果.   Future 一般由 ExecutorService 的submi ...

  2. 手贱!使用django,在数据库直接删除了表

    莫名其妙的错误. 删除了migreation文件,并且更换了数据库. 1.直接makemigrations + migrate    error: no change ??  WTF 2.makemi ...

  3. Tornado之链接数据库

    5 数据库 知识点 torndb安装 连接初始化 执行语句 execute execute_rowcount 查询语句 get query 5.1 数据库 与Django框架相比,Tornado没有自 ...

  4. python json.loads json.dumps(ensure_ascii = False) 汉字乱码问题解决

    python 转换为json时候 汉字编码问题 2017年03月23日 18:50:04 阅读数:5604 有这样一个需求: 需要一个json 文件 数据从数据库里查询出来 1. 设置文件头 # -* ...

  5. 2k8 32bit下载

    Windows Server 2008 32-bit Standard(标准版) Windows Server 2008 32-bit Enterprise(企业版) Windows Server 2 ...

  6. attack source code

    不废话,直接上代码, 先看截图use pictures;

  7. python入门-字典

    1 python是使用{}来表示字典 字典是一系列的键值对 alien_0={} 2 访问字典中的值 new_point = alien_0['point'] print("you just ...

  8. leetcode541

    public class Solution { public string ReverseStr(string s, int k) { var len = s.Length; //记录k的倍数 //分 ...

  9. JSP生成WORD文档,EXCEL文档及PDF文档的方法

    转自:https://www.jb51.net/article/73528.htm 本文实例讲述了JSP生成WORD文档,EXCEL文档及PDF文档的方法.分享给大家供大家参考,具体如下: 在web- ...

  10. DOS系统变量

    %ALLUSERSPROFILE% : 列出所有用户Profile文件位置.%APPDATA% : 列出应用程序数据的默认存放位置.%CD% : 列出当前目录.%CLIENTNAME% : 列出联接到 ...