I have a class that contains class attributes. I need that they are shared as class attributes by a group of other classes, so that if any of them modifies those attributes, they are modified for all of them. I naively tried it using inheritance, but it is not working like that: each class behaves as if it would have their own "copy" of those class attributes.
Files:
# baseClass.py
class BaseClass(object):
iden = 0
# child1.py
from baseClass import BaseClass
class Child1(BaseClass):
@classmethod
def getId(cls):
return cls.iden
# child2.py
from baseClass import BaseClass
class Child2(BaseClass):
@classmethod
def getId(cls):
return cls.iden
The file I am using to test this is:
from child1 import Child1
from child2 import Child2
print(Child1.getId())
Child1.iden = 5
print(Child1.getId())
print(Child2.getId())
The output I am seeing:
0
5
0
How should I do this, so that Child1, Child2 and BaseClass share the iden class attribute?
You can do this by adding an iden
property to the metaclass of BaseClass
:
class BaseType(type):
_iden = 0
def _get_iden(cls):
return BaseType._iden
def _set_iden(cls, value):
BaseType._iden = value
iden = property(_get_iden, _set_iden)
class BaseClass(object):
__metaclass__ = BaseType
class Child1(BaseClass):
pass
class Child2(BaseClass):
pass
print Child1.iden
Child1.iden = 5
print Child1.iden
print Child2.iden
Output:
0
5
5