Unexpected behaviour of getattr(obj, __dict__)

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Feb 14 06:36:29 EST 2006


I came across this unexpected behaviour of getattr for new style classes.
Example:

>>> class Parrot(object):
...     thing = [1,2,3]
...
>>> getattr(Parrot, "thing") is Parrot.thing
True
>>> getattr(Parrot, "__dict__") is Parrot.__dict__
False

I would have expected that the object returned by getattr would be the
same object as the object returned by standard attribute access. This is
true for some attributes, but not for __dict__. I don't know if there are
other examples.

Why is this? Is there some documentation I can read up about this? I've
tried searching, but can't find anything useful.



-- 
Steven.




More information about the Python-list mailing list