[Python-Dev] __class_init__

Thomas Heller thomas.heller@ion-tof.com
Thu, 15 Nov 2001 16:06:50 +0100


From: "Skip Montanaro" <skip@pobox.com>
>     >> [__class_init__]
> 
>     Tim> [Guido said] ...  "The right way" is to define a custom metaclass
>     Tim> instead (whose __init__ plays the role __class_init__ would have
>     Tim> played if defined in the class).  It makes sense to me, but I'll
>     Tim> have to play with it to be convinced it's as usable.
> 
> For those of us with next to no ExtensionClass experience, I think it would
> be helpful to describe what __class_init__ does.  I saw it there in the
> PyGtk 2.x wrappers before JamesH converted to new-style classes.  I had no
> idea what it did (and blissfully ignored it) and James apparently didn't
> need any similar functionality after the conversion.

__class_init__ (or was it called __init_class__?) is a *class* method
which is called when the *class* is created (the class statement executed).

class X(ExtensionClass.Base):
  def __class_init__(cls):
    print cls, "created"

class Y(X):
  pass

would first print '<extensionclass __main__.X> created' and then
'<extensionclass __main__.Y> created'.

It can be used to initialize class attributes for example depending
on other class attributes (that's at least what I have/had in mind).

Thomas