Multiple inheritance, super() and changing signature

Ian Kelly ian.g.kelly at gmail.com
Fri Jun 3 10:39:00 EDT 2016


On Fri, Jun 3, 2016 at 8:06 AM, Nagy László Zsolt <gandalf at shopzeus.com> wrote:
>
>>> That's overly strict. As Raymond shows, it is easy to deal with
>>> changing method signatures in *cooperative* classes.
>> I must watch that for sure.
>
> All right, I have read this:
>
> https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
>
> There is still something I don't get: how to create cooperative classes
> when some base classes share some of the parameters?

Why do they need to share the same parameter? Part of cooperative
design is that you can't really design each class in a vacuum; you
need to take the other classes they might get combined with into
account. Perhaps you can extract that parameter into another base
class that is shared by both:

class Root:
    def __init__(self, *, param1, **kwds):
        self.param1 = param1
        super().__init__(**kwds)

class A(Root):
    def __init__(self, *, param2, **kwds):
        self.param2 = param2
        super().__init__(**kwds)

class B(Root):
    def __init__(self, *, param3, **kwds):
        self.param3 = param3
        super().__init__(**kwds)

class X(A, B):
    pass

A and B can both depend on having param1 without stomping on each
other because they both inherit Root which requires it.


More information about the Python-list mailing list