[Python-Dev] __class_init__

Thomas Heller thomas.heller@ion-tof.com
Fri, 16 Nov 2001 17:56:34 +0100

From: "Tim Peters" <tim.one@home.com>
[__class_init__ methods]
> I pinged Guido about this (he'll fire me if I ever do that again <wink>),
> and he's really not keen on it.  "The right way" is to define a custom
> metaclass instead (whose __init__ plays the role __class_init__ would have
> played if defined in the class).  It makes sense to me, but I'll have to
> play with it to be convinced it's as usable.

It seems Guido is right ;-), it is easier than ever!

Python 2.2b1+ (#25, Nov  6 2001, 21:18:43) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class metaclass(type):
...   def __init__(self, *args):
...     type.__init__(self, *args)
...     try:
...       cls_init = getattr(self, '__init_class__')
...     except AttributeError:
...       pass
...     else:
...       cls_init.im_func(self)
>>> meta = metaclass('meta', (), {})
>>> class X(meta):
...   def __init_class__(self):
...     print "__init_class__", self
__init_class__ <class '__main__.X'>
>>> class Y(X):
...   pass
__init_class__ <class '__main__.Y'>
>>> ^Z

Now I'll have to find out how to convert this to C.