0

I'm currently using this pattern to create a class C that inherits from A and B. I couldn't call super().__init__ from C since I would have to do the same in A and B, and the unexpected parameter would cause problems at the top level. I feel like this isn't very elegant. What is the proper way to do multiple inheritance in Python? I guess it is unusual to query the mro to find out if the superclass expects a parameter?

class A:
    def __init__(self, something):
        self.a = X(something)

    def method_a(self):
        self.a.go()

    def method_ab(self):
        self.a.go2()


class B:
    def __init__(self, something):
        self.b = X(something)

    def method_b(self):
        self.b.go()

    def method_ab(self):
        self.b.go2()


class C(A, B):
    def __init__(self, something):
        self.a_ = A(something)
        self.b_ = B(something)

    @property
    def a(self):
        return self.a_.a

    @property
    def b(self):
        return self.b_.b

    def method_ab(self):
        for x in [self.a, self.b]:
            x.method_ab()
4
  • 2
    Relevant read: Python’s super() considered super! Commented Jul 12, 2013 at 10:07
  • 1
    In your last example you aren't using inheritance at all. I mean, yes C inherits A and B, but the class body is using delegation/composition(i.e. doing class C: wouldn't change the behaviour of the class, except for isinstance checks[actually it's possible to do some hacks to make them work too]) Commented Jul 12, 2013 at 12:21
  • @Bakuriu: Actually, method_a and method_b are being inherited… although I admitted that it wasn't elegant. See my solution below. Commented Jul 12, 2013 at 16:12
  • @delnan: Yes, that is totally relevant! Most important para: “For cases where object doesn't have the method of interest (a draw() method for example), we need to write a root class that is guaranteed to be called before object. The responsibility of the root class is simply to eat the method call without making a forwarding call using super().” Commented Jul 12, 2013 at 21:14

1 Answer 1

1

The best solution I found was to use a base class to absorb the extra parameters:

class Base:
    def __init__(self, something):
        pass

    def method_ab(self):
        pass

class A(Base):
    def __init__(self, something):
        super().__init__(something)
        self.a = X(something)

    def method_a(self):
        self.a.go()

    def method_ab(self):
        super().method_ab()
        self.a.go()


class B(Base):
    def __init__(self, something):
        super().__init__(something)
        self.b = X(something)

    def method_b(self):
        self.b.go()

    def method_ab(self):
        super().method_ab()
        self.b.go()


class C(A, B):
    pass
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.