I am having some trouble to understand python new format class multiple heritage. I have read quite a few documents and tried some codes, yet I can't really get how it works on some cases.
Looking at this code for instance:
class A(object): def x(self): print "X from A." def __init__(self): print "A here." class B(object): def x(self): print "X from B." def __init__(self): print "B here." class C(A, B): def __init__(self): super(C, self).__init__() super(A, self).__init__() print "C here." super(C, self).x() super(A, self).x() c = C()
The output is:
X from A.
X from B.
So, it seems that calling super with "C" as the class will find the first of the parent class that has the method, and use that method (which is fine, IMO). The problem is if you want to call the init method of B class, you must call super(A, self).__init__(), instead of B. This really doesn't make sense to me.
It gets worse, if you call super(B, self).__init__(), it does nothing! No error, no warning, no function call, no nothing, is like the line is a "pass" statement. And it gets even worse if you try calling the x method. If you want to call x from B, you use super(A, self).x, that is somewhat aligned with how you can B's __init__ method. But, if you try super(B, self).x(), you get an error: AttributeError: 'super' object has no attribute 'x'
So my questions would be:
1) Why do you need to pass A to super to call B's methods? Would it be something like "start searching for a method after this point" or something?
2) Why calling __init__ passing B to super does nothing, but calling x raises an exception?
Thanks in advance.
I realised why the stroked text occour, it is looking for the methods on the object class.
Also, calling super with any other class name that is not the class itself makes pylint sad:
"E: 18, 2: Bad first argument 'A' given to super() (bad-super-call)"
I am not really sure why python would allow you to give an argument that can't vary, but pylint does complain about it, pyflakes doesn't say a thing.