Unexpected behaviour of getattr(obj, __dict__)

Fredrik Lundh fredrik at pythonware.com
Tue Feb 14 07:40:05 EST 2006


Steven D'Aprano wrote:

> >>>>>class Parrot(object):
> >>
> >> ...     thing = [1,2,3]
> >> ...
> >>
> >>>>>getattr(Parrot, "thing") is Parrot.thing
> >>
> >> True
> >>
> >>>>>getattr(Parrot, "__dict__") is Parrot.__dict__
> >>
> >> False
> >
> >
> > hint:
> >>>> getattr(object, '__dict__')
> > <dictproxy object at 0x2aaaaab2ff30>
>
> That doesn't answer the question, it just re-words it. Why is the
> dictproxy returned by getattr a different instance from the dictproxy
> that you get when you say object.__dict__?

because it's created on the fly:

>>> Parrot.__dict__
<dictproxy object at 0x009818B0>
>>> Parrot.__dict__
<dictproxy object at 0x00981A10>
>>> Parrot.__dict__
<dictproxy object at 0x009818B0>
>>> Parrot.__dict__
<dictproxy object at 0x00981A10>
>>> Parrot.__dict__
<dictproxy object at 0x009818B0>
>>> Parrot.__dict__
<dictproxy object at 0x00981A10>
>>> Parrot.__dict__
<dictproxy object at 0x009818B0>
>>> Parrot.__dict__
<dictproxy object at 0x00981A10>
>>> Parrot.__dict__
<dictproxy object at 0x009818B0>
>>> Parrot.__dict__
<dictproxy object at 0x00981A10>
>>> Parrot.__dict__
<dictproxy object at 0x009818B0>

the object itself contains a dictionary.

</F>






More information about the Python-list mailing list