Classes with initialization

Carl Banks pavlovevidence at gmail.com
Mon Apr 9 19:16:11 EDT 2007


On Apr 9, 3:26 am, mariano.suarezalva... at gmail.com wrote:
> Hi all,
>
> I'm currently using code similar to this:
>
>   class ClassWithInitialization(type):
>       def __init__(cls, name, bases, dict):
>           type.__init__(name, bases, dict)
>           dict['__class_init__'](cls)
>
>   class A:
>       __metaclass__ = ClassWithInitialization
>
>       def __class_init__(cls):
>           cls.some_attribute = ...
>           ...
>
> in order to get class attributes initialized (since the values of
> these attributes
> need non trivial work to be computed, putting the code that does that
> computation in the class scope ends up with the class having extra
> attributes---the `local' variables used in the computation of the
> values of class attribute; so I'm using __class_init__'s scope to
> contain those variables)
>
> I was wondering: is there a simpler approach to this?

You could put the computations right in the class, and del the extra
variables when done.


Carl Banks




More information about the Python-list mailing list