this is the first time I write here, sorry if the message is unfocuessed or too long.
I was interested in understanding more about how objects'attributes are fetched when needed. So I read the Python 2.7 documentation titled "Data Model" here, I met __getattr__
and, in order to check whether I understood or not its behavior, I wrote these simple (and incomplete) string wrappers.
class OldStr:
def __init__(self,val):
self.field=val
def __getattr__(self,name):
print "method __getattr__, attribute requested "+name
class NewStr(object):
def __init__(self,val):
self.field=val
def __getattr__(self,name):
print "method __getattr__, attribute requested "+name
As you can see they are the same except for being an oldstyle vs newstyle classes. Since the quoted text says __getattr__
is "Called when an attribute lookup has not found the attribute in the usual places", I wanted to try a + operation on two instances of those classes to see what happened, expecting an identical behavior.
But the results I got puzzled me a little bit:
>>> x=OldStr("test")
>>> x+x
method __getattr__, attribute requested __coerce__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
Good! I did not define a method for __coerce__
(although I was expecting a request for __add__
, nevermind :), so __getattr__
got involved and returned a useless thing. But then
>>> y=NewStr("test")
>>> y+y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NewStr' and 'NewStr'
Why this asymmetric behavior between __getattr__
in oldstyle classes and newstyle ones when using a built-in operator like +? Could someone please help me to understand what is going on, and what was my mistake in reading the documentation?
Thanks a lot, your help is strongly appreciated!
See Special method lookup for new-style classes.
Special methods are directly looked up in the class object, the instance objects and consequently __getattr__()
and __getattribute__()
are bypassed. For precisely the same reason, instance.__add__ = foobar
doesn't work.
This is done to speed up attribute access. Special methods are called very frequently, and making them subject to the standard, rather complex attribute lookup would significantly slow down the interpreter.
Attribute lookup for old-style classes is much less complex (most notably old style classes do not support descriptors), so there is no reason to treat special methods differently in old style classes.
Your __getattr__
functions aren't returning anything. I don't know why the old-style class is doing the __getattr__
before looking for __add__
, but it is, and this is trying to call the return value which is None
.
The new-style class is doing it right: you haven't defined __add__
so it doesn't know how to add them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With