0

I am trying to to run the following piece of code, and I want C to initialize both A and B (and possible more) in a neat and dynamic manner.

class A(object):
    def __init__(self):
        print 'Class A init'

class B(object):
    def __init__(self):
        print 'Class B init'

class C(A, B):
    def __init__(self):
        super(C, self).__init__()
        print 'Class C init'

c = C()

However, this only outputs

 Class A init
 Class C init

I know that this is due to the depth-first search manner when invoking super(...), and it stops when first method was found. Can I in some way initialize all super classes?

Edit: Defining C as below achieves the goal of initializing all super classes. Is there a way to implicit initialize all super classes?

class C(A, B):
    def __init__(self):
        A.__init__(self)
        B.__init__(self)
        print 'Class C init'

Solution: As pointed out a super(B, self).__init__() was added in class B

5
  • Why don't you call super(B, self).__init__() in B ? Can't think of a design where you really would want C to do both... Commented Mar 1, 2015 at 12:07
  • 1
    duplicate of stackoverflow.com/q/8688114/3004881 Commented Mar 1, 2015 at 12:09
  • B does not inherit A Commented Mar 1, 2015 at 12:09
  • @JonClements: I stand corrected. That worked. I did not know that. Commented Mar 1, 2015 at 12:11
  • This is a pretty bad way of inheriting. Why not just use a mixin for the behavior that you do want? Commented Mar 1, 2015 at 12:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.