0% found this document useful (0 votes)
278 views

Python Inheritance - Is It Necessary To Explicitly Call The Parents Constructor and Destructor - Stack Overflow

The document discusses whether it is necessary to explicitly call the parent constructor and destructor methods in a Python subclass. The answers indicate that: 1) If a subclass does not define its own __init__ or __del__ methods, the parent methods will be called automatically. 2) If a subclass wants to initialize variables or call functionality in the parent class, it should explicitly call the parent's __init__ method with super(). 3) An example shows a subclass inheriting the parent's __del__ method without overriding or explicitly calling it.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
278 views

Python Inheritance - Is It Necessary To Explicitly Call The Parents Constructor and Destructor - Stack Overflow

The document discusses whether it is necessary to explicitly call the parent constructor and destructor methods in a Python subclass. The answers indicate that: 1) If a subclass does not define its own __init__ or __del__ methods, the parent methods will be called automatically. 2) If a subclass wants to initialize variables or call functionality in the parent class, it should explicitly call the parent's __init__ method with super(). 3) An example shows a subclass inheriting the parent's __del__ method without overriding or explicitly calling it.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

9/21/2020 Python Inheritance: Is it necessary to explicitly call the parents constructor and destructor?

- Stack Overflow

Python Inheritance: Is it necessary to explicitly call the parents


constructor and destructor?
Asked 4 years, 5 months ago Active 4 years, 1 month ago Viewed 5k times

I have some Code (for ev3dev):

9 class Motor(object):
def __init__(self, portName):
self.base = "/sys/class/tacho-motor/motor"
self.number = self.getMotorNumberWithSpecificPortName(portName)
self.name = self.base + str(self.number) + "/"
1
self.setDefaultValues()

def __del__(self):
self.callReset()

(...)

class TurnMotor(Motor):
def __init__(self):
super(TurnMotor, self).__init__("outA")

def __del__(self):
super(TurnMotor, self).__del__()

The goal is to define multiple motor classes like TurnMotor in this example who inherit from Motor
and automatical __init__ with their specific port. They also should call the parents __del__
method on destruction to reset the motor.

I know that in this case I have to define a __init__ method for the subclass to initiate with the
port I want but would the parents __del__ method still be called from the subclass if I leave out
the definition for __del__ in the subclass?
Would this in general be possible for __init__ too?

Thanks in advance and sorry if this is a duplicate.

python inheritance mindstorms

asked Mar 31 '16 at 11:30


monsterkrampe
181 2 10

2 Answers Active Oldest Votes

https://stackoverflow.com/questions/36332167/python-inheritance-is-it-necessary-to-explicitly-call-the-parents-constructor-a#:~:text=Yes it is possible.,… 1/2


9/21/2020 Python Inheritance: Is it necessary to explicitly call the parents constructor and destructor? - Stack Overflow

__init__and __del__ are merely construction and destruction hooks, although this statement
7 can be subject to discussion.

What is important for the programmer is that you do not have to define the super-class
con/desctructor. If you don't, Python looks for a con/destructor in the internal lookup chain, i.e. in
the base class(es), according to the method resolution order (MRO).

If you want a con/destructor in the derived class and if you want to create or destroy instance
variables of the super class (and most of the time you want to do that, as that is why you derived
in the first place) or do whatever is done in the super classes con/destructor, then you will have to
call the super classes methods accordingly.

This is not done explicitly because (a) Python gives you the chance of not doing it and (b) Python
Zen says: "Explicit is better than implicit".

edited Aug 19 '16 at 5:32 answered Mar 31 '16 at 11:50


steffen
6,998 8 36 73

Yes it is possible. If you do not overwrite a method within subclass the parent method will be
invoked instead. This is also true for magic methods like __del__ or __init__ .
10
Here a small example I run in python cli

>>> class A():


... def __del__(self):
... print('A.__del__')
...
>>> class B(A): pass
...
>>> b = B()
>>> del b
A.__del__

Class B will contain all information of class A mixed with its specific information.

answered Mar 31 '16 at 11:43


keksnicoh
2,242 11 25

https://stackoverflow.com/questions/36332167/python-inheritance-is-it-necessary-to-explicitly-call-the-parents-constructor-a#:~:text=Yes it is possible.,… 2/2

You might also like