Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are parent constructors not called when instantiating a class? [duplicate]

class A:
    def __init__(self):
        print 'A'


class B(A):
    def __init__(self):
        print 'B'


b = B()
B

In C++, I would have expected to see A B output, but in Python I am getting only B. I know that I can do super(B, self).__init__() to achieve the same in Python, but as this is apparently not the default (or is it - I am new to the syntax as well), I am worried that the paradigms for instatinating objects are completely different.

So what are objects in Python, what is their relation with classes and what is the standard way to initialize all data in all parent classes in Python?

like image 862
Vorac Avatar asked Jun 12 '13 10:06

Vorac


People also ask

Does a child class always call the parent constructor?

That depends on what you mean by "use." If you mean, does the default constructor for a child class call the parent constructor, then yes, it does (more below). If you mean, is a default constructor matching whatever parameters the parent constructor has created automatically, then no, not in the general case.

Why is parent constructor called automatically?

Before you can initialize an object in a constructor, the object's parent constructor must be called first. If you don't write an explicit call to the super() constructor, Java automatically inserts one in your constructor. The compiler automatically inserts superclass constructor calls in both constructors.

Does child class call parent constructor in Java?

If the child class constructor does not call super , the parent's constructor with no arguments will be implicitly called. If parent class implements a constructor with arguments and has no a constructor with no arguments, then the child constructors must explicitly call a parents constructor.

Why constructors are not inherited and override?

In simple words, a constructor cannot be inherited, since in subclasses it has a different name (the name of the subclass). Methods, instead, are inherited with "the same name" and can be used.


1 Answers

Python rarely does anything automatically. As you say, if you want to invoke the superclass __init__, then you need to do it yourself, usually by calling super:

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

The point to note is that instance attributes, like everything else in Python, are dynamic. __init__ is not the constructor, that's __new__ which you rarely need to meddle with. The object is fully constructed by the time __init__ is called, but since instance attributes are dynamic they are usually added by that method, which is only special in that it's called first once the object is created.

You can of course create instance attributes in any other method, or even from outside the class itself by simply doing something like myBobj.foo = 'bar'.

like image 50
Daniel Roseman Avatar answered Nov 14 '22 22:11

Daniel Roseman