2

I am a Java programmer, beginner in Python programming. I have noticed unexpected behavior in python programming. I was expecting the print sequence as B class ,A Class constructors. But it's executing constructor of A only.

Output as "Its Constructor of A",Could you please help me to understand flow of execution. Thanks in advance

class B:
    def __init__(self):
        print 'Its constructor of B'    

class A(B):
    def __init__(self):
        print 'Its constructor of A'
        #B.__init__(self)

if __name__=='__main__':
    obj=A()
Zac
  • 2,180
  • 2
  • 23
  • 36
user1559873
  • 6,650
  • 8
  • 25
  • 28
  • 3
    That's just the way Python works. IIRC, even in Java, if you're extending a class whose constructor takes parameters, you have to explicitly call `super()`. The only difference is that for a superclass which doesn't take params, calling `super()` is optional in Java, whereas in Python you have to call it explicitly. – Aya May 19 '13 at 11:58
  • Explicit is better than implicit. http://www.python.org/dev/peps/pep-0020/ – Lennart Regebro May 19 '13 at 12:05
  • See also [Understanding Python super() and init methods](http://stackoverflow.com/questions/576169/understanding-python-super-and-init-methods) – Lennart Regebro May 19 '13 at 12:09

1 Answers1

2

In python you should call the parent's initializer (that's how the __init__ method is actually called, - "constructor" is something else) explicitly.

You can do it like you did in the commented out line. Better yet, you should use the super function, that figures out, which parent to access for you. It only works with new-style classes though (basically, that means, that the root of your class hierarchy must inherit from object).

shylent
  • 10,076
  • 6
  • 38
  • 55
  • 1
    What's the difference between a constructor and initializer? Seems to be rather identical to me. Answered my own question: init is called a constructor expression in the python data model: http://docs.python.org/2/reference/datamodel.html#basic-customization – Voo May 19 '13 at 12:39
  • 1
    @Voo: I am sorry, but your statement regarding constructor expression is incorrect. The constructor expression is the "call to the class" as per http://docs.python.org/2/reference/datamodel.html#basic-customization. ``__init__`` is different from the "constructor" you might be used to from c++ and java, because it does not construct anything: the instance of the class is already present at the time it is called. – shylent May 19 '13 at 13:00
  • @Voo: The sentence you misinterpreted merely states that `__init__` receives the same arguments that `__new__` received. – chepner May 19 '13 at 13:13
  • @chepner It says `As a special constraint on constructors, no value may be returned` in that paragraph. That certainly does *not* refer to `__new__` since `__new__` is just fine (in fact rather necessary) with returning an object.. – Voo May 23 '13 at 19:00
  • @Voo Touché. That documentation should be updated. It is possibly left over from the days before `__new__` became available to override. (The following statement needs to be fact-checked:) Although `__init__` has always been simply an initializer, in version 1.x, there was no explicit constructor at the Python level, so one could be loose in describing how `__init__` worked. – chepner May 23 '13 at 19:06
  • @chepner Well compare `Foo.__init__` to `Foo()`. You may say that `__init__` does not actually allocate memory and that that's __new__'s job which distinguishes it from the c++ version. But neither does the constructor in c++ allocate the memory, it just gets handed the address of an uninitialized chunk of memory. C++ just doesn't give us an option to change the allocation behavior. – Voo May 23 '13 at 22:55