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?