Automatic class attribute

Franck PEREZ franck.perez at gmail.com
Fri Feb 3 17:28:08 EST 2006


Hello all,

Considering the following code :

class C(object):
   ...:     observers = []
   ...:
   ...:     @classmethod
   ...:     def showObservers(cls):
   ...:         print cls.observers

class D1(C):
   ...:     observers = [] #could it be moved in C ?

class D2(C):
   ...:     observers = [] #could it be moved in C ?

I want each child class of C to have it's own "observers" class attribute.

The code I provided works... but I'd like to avoid typing "observers =
[]" in each child class.

Is it possible to define something in C which would basically mean :
"for each child class, automatically bind a new list attribute called
observers" ?

Are metaclasses a way ? Is it possible to avoid them ?
Thanks a lot,
Franck



More information about the Python-list mailing list