0

I have the following code:

class TestA(object):
    def __init__(self, *args):
        super(TestA, self).__init__()
        print 'TestA'

class TestB(object):
    def __init__(self, my_var=None):
        super(TestB, self).__init__()
        print 'TestB', my_var

class TestC(object):
    def __init__(self, my_var=None):
        super(TestC, self).__init__()
        print 'TestC', my_var

class TestD(TestB, TestA, TestC):
    def __init__(self):
        print 'TestD'
        super(TestD, self).__init__("Hello World")

TestD() # Start from here

And the output is

TestD
TestC None
TestA None
TestB Hello World

I would expect to have :

TestD
TestB Hello World
TestA Hello World
TestC Hello World

What's wrong in my code or my understanding?

If I remove super on TestA, TestB, TestC and I call 3 times super like below, I have the expected output. (But I don't want to call three times super)

class TestD(TestB, TestA, TestC):
    def __init__(self):
        print 'TestD'
        super(TestD, self).__init__("Hello World")
        super(TestD, self).__init__("Hello World")
        super(TestD, self).__init__("Hello World")

Thank you in advance.

M07
  • 1,060
  • 1
  • 14
  • 23
  • Your methods aren't forwarding any arguments when they use `super`. – user2357112 Aug 18 '17 at 19:53
  • Now look here, `super` is nothing special. It lets you access your parent class's attributes. That's it. That's all it does. It doesn't magically call _all_ parent classes' constructors (which is how you wrote your code in your previous question), nor does it magically insert arguments into your `__init__()` call with no arguments. – Aran-Fey Aug 18 '17 at 19:57
  • Super looks to call the TestA, TestB, TestC __init__ functions, but only TestB receives the parameter "Hello World". I do not see magic with this, but it inserts arguments into my __init__(). Otherwise I could not print it. – M07 Aug 18 '17 at 20:05
  • No, it doesn't do that for you. _You_ are passing an argument here: `super(TestD, self).__init__("Hello World")`. That's why TestB receives an argument. But then in TestB you do this: `super(TestB, self).__init__()` and that's why TestA doesn't get any arguments. – Aran-Fey Aug 18 '17 at 20:08
  • If you have more questions about `super`, I suggest you join us in [the python chat room](https://chat.stackoverflow.com/rooms/6/python). It's easier to answer questions like yours there. And it's better than getting posting more questions and getting them closed as duplicates. – Aran-Fey Aug 18 '17 at 20:12
  • Ok thanks for the tip, I will ask there. I understand better how it works but i don't see a way to solve my issue. – M07 Aug 18 '17 at 20:14

0 Answers0