Automatic class attribute

F

Franck PEREZ

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
 
K

Kirk McDonald

Franck said:
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

By an astounding coincidence, I was just working on a similar problem.
Metaclasses can do this, no problem:

class M(type):
def __init__(cls, name, bases, dict):
cls.observers = []

def showObservers(cls):
print cls.observers

class C(object):
__metaclass__ = M

class D1(C): pass
class D2(C): pass

-Kirk McDonald
 
F

Franck PEREZ

Franck said:
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

By an astounding coincidence, I was just working on a similar problem.
Metaclasses can do this, no problem:

class M(type):
def __init__(cls, name, bases, dict):
cls.observers = []

def showObservers(cls):
print cls.observers

class C(object):
__metaclass__ = M

class D1(C): pass
class D2(C): pass

-Kirk McDonald


Works great. Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top