I have a hierarchy of entities that should contain dictionaries with metadata. Any sub-class should inherit its ancestor dictionary and be able to add some key-values (delete inherit keys is not needed). The easiest solution I can think about is this:
class A(object):
def __init__(self):
self.doc = A.generate_doc()
@staticmethod
def generate_doc():
return {'a': 0}
class B(A):
def __init__(self):
super().__init__()
self.doc.update(B.generate_doc())
@staticmethod
def generate_doc():
return {'b': 0}
class C(B):
def __init__(self):
super().__init__()
self.doc.update(C.generate_doc())
@staticmethod
def generate_doc():
return {'c': 0}
print(A().doc) # {'a': 0}
print(B().doc) # {'a': 0, 'b': 0}
print(C().doc) # {'a': 0, 'b': 0, 'c': 0}
Is this a good design? Could the update() blocks be implicit maybe? I have more than one document in real code, so that would be nice.