Difference between Father. __init(self) and super(Father, self). __init__()

Asked

Viewed 649 times

3

I have a little doubt about the issue of inheritance in python.

In the following test, I hoped that quack prints the same value for the two classes I created:

class A(object):
    a,b = (None, None)
    def __init__(self):
        self.a = 'a'

    def quack(self):
        return 'retorno "%s" ' % self.a

class B(A):

    def __init__(self):
        super(A, self).__init__()
        self.b = 'b'

My test was done like this:

b = B()
a = A()

print(b.quack())
print(a.quack())

The exit was:

retorno "None" 
retorno "a"

But I thought the two should return to the same thing.

The inheritance of property a only worked as I expected when I changed the section of super for that reason:

A.__init(self)

Return:

retorno "a"
retorno "a"

Having these two cases, what is the difference between the two statements I created?

Why didn’t the super use the parent class method?

  • Your exit is not changed no? The return None is for class B and the a is for class A, right?

  • 'Cause my print is reversed, I’m posting it

  • @mgibsonbr realized that when I changed to super(B, self) worked. I guess I didn’t understand why the super server :\

  • Haha didn’t see your comment, and I ended up posting a reply saying exactly that... P

1 answer

3

The problem is that the super should not be called in the parent class, but in the parent class itself:

class B(A):
    def __init__(self):
        super(B, self).__init__()
        self.b = 'b'

For the idea of super is not to call the method in a specific class (for this the second form you used is the most indicated), but rather to call the method using the same python inheritance rules, just by defacing the current class:

class A(object): ...
class B(A): ...
class C(A): ...
class D(B, C): ...
class E(B): ...

class F(D, E):
    def __init__(self):
        super(F, self).__init__()

This example above will try to find a method __init__ in all classes of the hierarchy, except for own F. And the order he seeks is defined by the process of inheritance (which unfortunately I do not know...). That would be for any other method, too, like super(F, self).foo().

class F(D, E):
    def __init__(self):
        super(E, self).__init__()

If you used this code, on the other hand, Python would only get one __init__ in the superclasses of E, that is to say, B and then A. Even though C have a __init__, and C be one of the superclasses of F, he would not be sought in this case. And in particular, the E is not sought (because the super assumes it’s to ignore E, just as he ignored F in the previous example).

  • P.S. I’m sure that right here at Sopt has a question explaining the search order of the methods in the presence of multiple inheritance, but I’m not able to find to leave reference.

  • Just to complement: Python does not require that the writing of the method parameters of classes inherited in the same way as in PHP? I mean, if the method __init__ has the parameters a and b in the class A, it will not be mandatory in class B(A)?

  • 1

    No, in fact I didn’t even know that in PHP it was like this... I don’t know any other language that requires it, by the way. In Java, for example, subclasses can have the methods - and constructors - they want, the only thing required is that each constructor calls an implicit or explicitly valid subclass constructor. Python not even that. And as for the other methods, if you have the same parameters is override, otherwise it is Overload (in Java, in Python everything is override, since Python is not statically typed).

  • 1

    The consequence is that in Python it is easy to inherit from a class but make the daughter class incompatible with the mother class... If the programmer wants to inherit respecting the Principle of Liskov’s Substitution - or the other principles of SOLID - this is on his account, the language itself does not impose it. P.S. in the previous comment I meant "call a constructor of the superclass", not "sub".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.