#!/usr/bin/python3 # -*- coding: utf-8 -*- """ If you define __init__ in the child, when the constructor for a derived class is called, the constructor for the base class is NOT automatically called, but you can call it explicitly if you want. _init__() is not really a constructor. Many naive Python programmers get confused with it, since __init__() gets called when we create an object. A closer inspection will reveal that the first parameter in __init__() is the object itself (object already exists). The function __init__() is called immediately after the object is created, and is used to initialize it In python, there is no "real" destructor, but something similar, i.e. the method __del__. It is called when the instance is about to be destroyed, and if there is no other reference to this instance. If a base class has a __del__() method that should be called, then the derived class's __del__() method, if any, must explicitly call it, to ensure the base __del__ is called. However, you probably should not be using this method for that purpose anyway: https://stackoverflow.com/questions/1481488/what-is-the-del-method-how-to-call-it """ # Overriding class Parent: # define parent class def my_method(self) -> None: print("Calling parent method") def __init__(self) -> None: print("Calling parent constructor") class Child(Parent): # define child class def my_method(self) -> None: print("Calling child method") def __init__(self, xinit: int) -> None: # constructor for class b # define and initialize an instance variable x self.x = xinit # call base class constructor Parent.__init__(self) # which is functionall the same as this: # super().__init__(self) # instance of child c = Child(4) # child calls overridden method, which hides the parent one, # though both really still exist c.my_method() # These can be accessed any time directly, but you have to pass in "self" manually: Child.my_method(c) Parent.my_method(c) # This is a name-resolution order process, # whereby the namespaces searched first are the child, then parents.