order of calling constructors in Python

2024/11/15 1:54:41
#!/usr/bin/pythonclass Parent(object):        # define parent classparentAttr = 100def __init__(self):print "Calling parent constructor"def parentMethod(self):print 'Calling parent method'def setAttr(self, attr):Parent.parentAttr = attrdef getAttr(self):print "Parent attribute :", Parent.parentAttrclass Child(Parent): # define child classdef __init__(self):print "Calling child constructor"def childMethod(self):print 'Calling child method'c = Child()          # instance of child

I have called created an instance of the Child class here.It doesn't seem to call the constructor of the parent class.The output looks like shown below.

Calling child constructor

In C++ for example when you call the constructor of a derived class the base class constructor get called first.Why is this not happening in Python?

Answer

From the Zen of Python:

Explicit is better than implicit.

Should Python call the parent constructor before or after the child constructor? With what arguments? Not knowing, it leaves it to you to decide.

class Child(Parent): # define child classdef __init__(self):super(Child, self).__init__()  # call the appropriate superclass constructorprint "Calling child constructor"

See also this StackOverflow post for the benefits of using super().

https://en.xdnf.cn/q/72020.html

Related Q&A

How do I access data from a python thread

I have a very simple threading example using Python 3.4.2. In this example I am creating a five threads that just returns the character string "Result" and appends it to an array titled thre…

How to tell if a full-screen application is running?

Is it possible in python to tell if a full screen application on linux is running? I have a feeling it might be possible using Xlib but I havent found a way.EDIT: By full screen I mean the WHOLE scree…

Pretty printers for maps throwing a type error

Ive configured pretty printers using http://wiki.eclipse.org/CDT/User/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F. It successfully works for vector and other containers. However I cant get …

Return PDF generated with FPDF in Flask

I can generate a PDF with an image using the code below. How can I return the generated PDF from a Flask route?from fpdf import FPDF pdf = FPDF() img = input(enter file name) g = img + .jpg pdf.add_p…

Tensorflow not found on pip install inside Docker Container using Mac M1

Im trying to run some projects using the new Mac M1. Those projects already work on Intel processor and are used by other developers that use Intel. I am not able to build this simple Dockerfile: FROM …

Fast fuse of close points in a numpy-2d (vectorized)

I have a question similar to the question asked here: simple way of fusing a few close points. I want to replace points that are located close to each other with the average of their coordinates. The c…

I use to_gbq on pandas for updating Google BigQuery and get GenericGBQException

While trying to use to_gbq for updating Google BigQuery table, I get a response of:GenericGBQException: Reason: 400 Error while reading data, error message: JSON table encountered too many errors, givi…

Something wrong with Keras code Q-learning OpenAI gym FrozenLake

Maybe my question will seem stupid.Im studying the Q-learning algorithm. In order to better understand it, Im trying to remake the Tenzorflow code of this FrozenLake example into the Keras code.My code…

How to generate month names as list in Python? [duplicate]

This question already has answers here:Get month name from number(18 answers)Closed 2 years ago.I have tried using this but the output is not as desired m = [] import calendar for i in range(1, 13):m.a…

Getting ERROR: Double requirement given: setuptools error in zappa

I tried to deploy my Flask app with zappa==0.52.0, but I get an error as below;ERROR: Double requirement given: setuptools (already in setuptools==52.0.0.post20210125, name=setuptools) WARNING: You are…