| fengxia's profilefengxia的共享空间PhotosBlogNetwork | Help |
|
|
March 29 the difference betwee __getattr__ and __getattribute__"i find it difficult to understand the difference between the magic methods __getattr__ and __getattribute__ and so donot know when to use former or later. can someone brief me on it ?
This is better understood with a bit of history. earlier Python versions (before 2.2) the only object model available was what we now call "classic classes". Classic instances hold their attributes in a dictionary, called __dict Attribute lookup starts at this instance dictionary; if not found, continues in the class, and its parent class, all along the inheritance tree. If still not found, __getattr__ (if it exists) is called, and should return the attribute value or raise AttributeError. That is, __getattr__ is called *last*, and *only* when the attribute was not previously found in the usual places.
Since Python 2.2, there are "new style classes" available; they inherit directly or indirectly from object. A new style instance may not even have a __dict An existing __getattribute__ method is tried *first*; it should return the attribute value or raise AttributeError. If no custom __getattribute__ exists, the default objectgetattribute__ is used. As a last resort, if __getattr__ is defined, it is called." TrackbacksThe trackback URL for this entry is: http://fengxiamu.spaces.live.com/blog/cns!D53B5914FF5CC497!149.trak Weblogs that reference this entry
|
|
|