How to get a reference of the 'owner' class to which a method belongs in Python 3.X?

Ian Kelly ian.g.kelly at gmail.com
Sun Mar 18 17:14:12 CET 2012


On Sun, Mar 18, 2012 at 3:42 AM, Cosmia Luna <cosmius at gmail.com> wrote:
> But it seems that the last line(#ref2) in the Py2Type.__init__ does not work at
> all.

I'm not sure what you're expecting it to do, but type.__init__ does
not actually do anything

> It seems really weird, 'type' is an instance of 'type' itself, I'm not sure
> if I'm calling the unbound method __init__ or bound method __init__.

type.__init__ is never bound.

> It seems type.__new__ calls type.__init__ before it calls Py2Type.__init__,
> because dict_ is already copied to cls.__dict__ at line(#ref3).

No, type.__new__ does not call type.__init__ at all.  Rather, it is
type.__new__ that is responsible for copying the dict, not
type.__init__.  For this reason you should override type.__new__ in
your metaclass, not type.__init__.

Cheers,
Ian



More information about the Python-list mailing list