Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

python - Asymmetric behavior for __getattr__, newstyle vs oldstyle classes

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 Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...