2007年07月09日 星期一 18:45
ʵÔÚ²»Ã÷°×Á½ÕßµÄÇø±ð£º __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. __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)". Çë¸ßÊÖÖ¸µãÒ»ÏÂ~£¡~£¡ -- ×¢ÒâÉíÌ壬ÉíÌåÊǸïÃüµÄ±¾Ç®£¡£¡ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070709/fd0baefa/attachment.html
2007年07月09日 星期一 19:44
Python 2.5.1 Stackless 3.1b3 060516 (release25-maint, May 23 2007, 12:31:42) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class Foo(object): ... def __init__(self): ... self.d = {'a': 1, 'b': 2} ... def __getattribute__(self, name): ... return self.d[name] ... >>> foo = Foo() >>> print foo.a File "", line 5, in __getattribute__ ... File " ", line 5, in __getattribute__ File " ", line 5, in __getattribute__ RuntimeError: maximum recursion depth exceeded >>> class Bar: ... def __init__(self): ... self.d = {'a': 1, 'b': 2} ... def __getattr__(self, name): ... return self.d[name] ... >>> bar = Bar() >>> print bar.a 1 答案是: 如果没有特殊理由, 永远不要用 __getattribute__ 。 在 07-7-9,jessinio smith<jessinio在gmail.com> 写道: > 实在不明白两者的区别: > > > > __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. > > > __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)". > > > > 请高手指点一下~!~! > > > -- > 注意身体,身体是革命的本钱!! > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > --- 努力 (沈崴学习 Python 中 ...) http://blog.163.com/eishn
2007年07月09日 星期一 20:02
少用"__getattribute__ " ----- Original Message ----- From: "沈崴" <wileishn在gmail.com> To: <python-chinese在lists.python.cn>; <python-cn在googlegroups.com> Sent: Monday, July 09, 2007 7:44 PM Subject: Re: [python-chinese]__getattr__和__getattribute__ > Python 2.5.1 Stackless 3.1b3 060516 (release25-maint, May 23 2007, > 12:31:42) > [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> class Foo(object): > ... def __init__(self): > ... self.d = {'a': 1, 'b': 2} > ... def __getattribute__(self, name): > ... return self.d[name] > ... >>>> foo = Foo() >>>> print foo.a > File "", line 5, in __getattribute__ > ... > File "", line 5, in __getattribute__ > File "", line 5, in __getattribute__ > RuntimeError: maximum recursion depth exceeded >>>> class Bar: > ... def __init__(self): > ... self.d = {'a': 1, 'b': 2} > ... def __getattr__(self, name): > ... return self.d[name] > ... >>>> bar = Bar() >>>> print bar.a > 1 > > 答案是: 如果没有特殊理由, 永远不要用 __getattribute__ 。 > > > 在 07-7-9,jessinio smith<jessinio在gmail.com> 写道: >> 实在不明白两者的区别: >> >> >> >> __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. >> >> >> __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)". >> >> >> >> 请高手指点一下~!~! >> >> >> -- >> 注意身体,身体是革命的本钱!! >> _______________________________________________ >> python-chinese >> Post: send python-chinese在lists.python.cn >> Subscribe: send subscribe to >> python-chinese-request在lists.python.cn >> Unsubscribe: send unsubscribe to >> python-chinese-request在lists.python.cn >> Detail Info: >> http://python.cn/mailman/listinfo/python-chinese >> > > --- > 努力 (沈崴学习 Python 中 ...) > http://blog.163.com/eishn > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
2007年07月11日 星期三 10:34
__getattribute__是在访问对象属性时无条件被调用的为防止无穷递归,你不能在这个函数里用self.xxx的语句。 __getattr__在__getattribute__抛出AttributeError异常后被调用 >>> class foo(object): ... def __init__(self): ... self.a = 1 ... def __getattr__(self, name): ... print 'getattr' ... return object.__getattribute__(self, name) ... def __getattribute__(self, name): ... print 'getattribute' ... return object.__getattribute__(self, name) ... >>> f=foo() >>> f.a getattribute 1 >>> f.b getattribute getattr Traceback (most recent call last): File "", line 1, in File " ", line 6, in __getattr__ AttributeError: 'foo' object has no attribute 'b' >>> On 7/9/07, jessinio smith <jessinio at gmail.com> wrote: > 实在不明白两者的区别: > > > > __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. > > > __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)". > > > > 请高手指点一下~!~! > > > -- > 注意身体,身体是革命的本钱!! > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese >
Zeuux © 2025
京ICP备05028076号