How can I extend the __init__base class, add more arguments for analysis , without requiring super().__init__(foo, bar)each derived class?
class Ipsum:
""" A base ipsum instance. """
def __init__(self, foo, bar):
self.foo = flonk(foo)
grungole(self, bar)
self._baz = make_the_baz()
class LoremIpsum(Ipsum):
""" A more refined ipsum that also lorems. """
def __init__(self, foo, bar, dolor, sit, amet):
super().__init__(foo, bar)
farnark(sit, self, amet)
self.wibble(dolor)
The purpose of the example is to show that Ipsum.__init__significant processing is taking place, so it cannot be duplicated in each subclass; and you LoremIpsum.__init__need parameters fooand bar, processed exactly the same as Ipsum, but also have their own special parameters.
It also shows that if Ipsumyou need to change to accept a different signature, each derived class must also change not only its signature, but also how it calls the superclass __init__. It is unacceptably fragile.
- :
class Ipsum:
""" A base ipsum instance. """
def __init__(self, foo, bar, **kwargs):
self.foo = flonk(foo)
grungole(self, bar)
self._baz = make_the_baz()
self.parse_init_kwargs(kwargs)
def parse_init_kwargs(self, kwargs):
""" Parse the remaining kwargs to `__init__`. """
pass
class LoremIpsum(Ipsum):
""" A more refined ipsum that also lorems. """
def parse_init_kwargs(self, kwargs):
(dolor, sit, amet) = (kwargs['dolor'], kwargs['sit'], kwargs['amet'])
farnark(sit, self, amet)
self.wibble(dolor)
, LoremIpsum , ; Ipsum __init__ - .
: , . , .
, , foo bar, super().__init__(foo, bar)? , , , LoremIpsum .