Message60293
The lookup of special methods invoked implicitly by
syntax other than explicit instance.attribute changed
to *not* use __gettattr__ when normal lookup failed.
This is contrary to docs, which consistently say
__getattr__ is unchanged. New special method
__getattribute__ is also bypassed, contrary to
implication of doc-essay.
This was reported on c.l.p by Jan Decaluwe using
2.2.2 on Red Hat Linux. On Win98, 2.2.1, I get same
behavior (I added test of __getattribute__):
class Iold:
def __init__(self,ob):
self.ob = ob
def __getattr__(self,name):
return getattr(self.ob,name)
class Inew(object):
def __init__(self,ob):
self.ob = ob
def __getattr__(self,name):
return getattr(self.ob,name)
a = Iold(1) #2
b = Inew(1) #2
a.__add__(1) #2
b.__add__(1) #2
a+1 #2
b+1 #error
#TypeError: unsupported operand types for +: 'Inew'
and 'int'
Inew.__getattribute__ = Inew.__getattr__
b+1 #same error, no loop
#TypeError: unsupported operand types for +: 'Inew'
and 'int'
b.__add__(1) # WARNING starts 'infinite' loop
def _(self,other): print 'hello'
Inew.__add__ = _
b+1 #prints 'hello', __getattribute__ bypassed.
http://python.org/2.2/descrintro.html says:
"Note that while in general operator overloading
works just as for classic classes, there are some
differences. (The biggest one is the lack of support for
__coerce__; new-style classes should always use the
new-style numeric API, which passes the other
operand uncoerced to the __add__ and __radd__
methods, etc.) "
Was lookup change meant to be one of differences?
"There's a new way of overriding attribute access. The
__getattr__ hook, if defined, works the same way as it
does for classic classes: it is only called if the regular
way of searching for the attribute doesn't find it."
But it is different.
"But you can now also override __getattribute__, a
new operation that is called for all attribute
references."
Except for implicit special methods.
I did not classify discrepancy because I don't know
whether interpreter or docs are off.
|
|
Date |
User |
Action |
Args |
2008-01-20 09:55:51 | admin | link | issue643841 messages |
2008-01-20 09:55:51 | admin | create | |
|