Python.__getattr__Vs__getattribute__

__getattr__ Vs __getattribute__

 1 class Fish(object):
 2     
 3     def __getattr__(self, key):
 4         if key == 'color':
 5             print 'access color'
 6             return 'blue'
 7         else: 
 8             raise AttributeError
 9         
10     def __getattribute__(self, key):
11         print '__getattribute__ %s' %key
12         return object.__getattribute__(self, key)
13         
14 
15 if __name__ == "__main__":
16     f = Fish()
17     
18     print "First %s 
" %f.color
19     
20     print '-----------------------
'
21     
22     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.

---
原文地址:https://www.cnblogs.com/cwgk/p/3375260.html