Consider the following piece of code:
class ABC:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
class ABCD(ABC):
def __init__(self, abc, d):
# How to initialize the base class with abc in here?
self.d = d
abc = ABC(1, 2, 3)
abcd = ABCD(abc, 4)
print(abcd.a)
What is a Pythonic way of initializing the base class with abc? If I used
super().__init__(abc.a, abc.b, abc.c)
I would have to change ABCD everytime I add someting to ABC. What I could do is to use
self.__dict__.update(abc.__dict__)
However, this feels clumsy and will break when ABC uses a different underlying implementation than dict (e.g. __slots__). Are there some alternative ways?
ABCDat the beginning, you could do:__init__ (self, d, *args, **kargs)and thensuper().__init__(*args, **kargs).abcd = ABCD(4, abc.a, abc.b, abc.c)instead ofabcd = ABCD(4, abc), thus forcing the user ofABCDto unpackabcmanually.ABCD(4, 1, 2, 3)directly.