Confused about __prepare__

Chris Rebert clp2 at rebertia.com
Thu Apr 7 19:09:59 EDT 2011


On Thu, Apr 7, 2011 at 3:31 PM, andrew cooke <andrew at acooke.org> wrote:
>
> In the code below I use __prepare__ to change the class dictionary so that a tuple is stored in __setitem__().  Since __getitem__() removes the tuple I wasn't expecting any problems, but it seems that __init__ is being retrieved via some other mechanism.  Why?

I suspect you're being bitten by
http://docs.python.org/dev/reference/datamodel.html#special-method-lookup

In particular: "implicit special method lookup generally also bypasses
the __getattribute__() method".

__init__() is a special method.
The default __getattribute__() implementation consults an object's __dict__.
type.__new__() presumably does an implicit special method lookup for
__init__() behind the scenes.

Hence, your custom __dict__ is bypassed.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list