简体   繁体   中英

Base Class __init__ being called automatically from derived class

I am having a little hard time understanding why this is happening. So far from what I have read about python is that the initializer of a base class needs to be explicitly called by the derived class. This is one of the source that confirms my understanding. However the following example baffles me

class foo(object) :
  def __init__(self,par):
    print "Inside foo constructor"

class bar(foo):
  status_code = 302

b = bar(23)

In the above case the derived class does not have an initializer. So will it be correct for me to assume that the reason why the initializer of the base class is being called is because its being inherited by the derived class.

I believe that you have to explicitly call the base class's __init__ only if you define __init__ of the derived class. Otherwise it will be called automatically. This is actually exactly what the answer you link says.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM