More arguments in __init__ derived class than __init__ base class

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 .

+4
3

, , , , **kwds, .

, , dict , (, .__ init__ ):

class Shape:
    def __init__(self, shapename, **kwds):
        self.shapename = shapename
        super().__init__(**kwds)        

class ColoredShape(Shape):
    def __init__(self, color, **kwds):
        self.color = color
        super().__init__(**kwds)

cs = ColoredShape(color='red', shapename='circle')

. " " Super " . Pycon video.

:

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, dolor, sit, amet, **kwds):
        super().__init__(**kwds)
        farnark(sit, self, amet)
        self.wibble(dolor)

:

li = LoremIpsum(dolor=1, sit=2, amet=3, foo=4, bar=5) 

. __init__, .

+4

:

class Ipsum:  # 3.x-ism; in 2.x always inherit from object.
    def __init__(self, arg1, arg2, arg3):
        # etc...

class LoremIpsum(Ipsum):
    def __init__(self, arg4, arg5, *args, **kwargs):
        super().__init__(*args, **kwargs)  # 3.x-ism; in 2.x super() needs arguments.
        # Do stuff with arg4 and arg5

, . , , , .

, , , ( , super().__init__() , ). Hettinger () .

+2

__init__. , - . ? , ; . foo bar Lorem ( , ), Lorem.__init__() - , . foo bar ( ) , , , , .

0

Source: https://habr.com/ru/post/1656889/


All Articles