Python: Inheritance

NOTES

If no base class is defined for a class, then it is a subclass of “object“.

So, not defining base class is equivalent to – class MyClass(object)

NOTES

If a method implementation in the base class servers our purpose, and sufficient for our requirement, then we do not need to reimplement it in a subclass.

Inherit from Single Class

class BigBoxParent:
    def myParentMethOne(self):
        print("my Parent Meth One")
        
    def myParentMethTwo(self):
        print("my Parent Meth Two")
        
class BigBoxChild(BigBoxParent):
    def myChildMethodOne(self):
        print("My Child Method One")
        

child_obj = BigBoxChild()
child_obj.myChildMethodOne()
child_obj.myParentMethOne()

print(dir(child_obj))
Python

Output:

My Child Method One
my Parent Meth One


['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', 
'__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', 
'__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 
'myChildMethodOne', 'myParentMethOne', 'myParentMethTwo']
Plaintext

Inherit from Multiple Classes

class BigBoxParent:
    def myParentMethOne(self):
        print("my Parent Method One")
        
    def myParentMethTwo(self):
        print("my Parent Method Two")
        
class NewParent:
    def newParentMethOne(self):
        print("new Parent Method one")
        
    def newParentMethTwo(self):
        print("new Parent Method Two")
        
class BigBoxChild(BigBoxParent, NewParent):
    def myChildMethodOne(self):
        print("My Child Method One")
        

child_obj = BigBoxChild()
child_obj.myChildMethodOne()
child_obj.myParentMethOne()
child_obj.newParentMethTwo()

print(dir(child_obj))
Python

Output:

My Child Method One
my Parent Method One
new Parent Method Two


['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', 
'__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', 
'__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
'__weakref__', 'myChildMethodOne', 'myParentMethOne', 'myParentMethTwo', 
'newParentMethOne', 'newParentMethTwo']
Plaintext

__init__ Execution in Inheritance

class BigBoxParent:
    def __init__(self):
        print("BigBoxParent __init__")

    def myParentMethOne(self):
        print("my Parent Method One")
        
    def myParentMethTwo(self):
        print("my Parent Method Two")
        
class NewParent:
    def __init__(self):
        print("NewParent __init__")
        
    def newParentMethOne(self):
        print("new Parent Method one")
        
    def newParentMethTwo(self):
        print("new Parent Method Two")
        
class BigBoxChild(BigBoxParent, NewParent):
    def __init__(self):
        super().__init__()
        print("BigBoxChild __init__")
        
    def myChildMethodOne(self):
        print("My Child Method One")
        

child_obj = BigBoxChild()
child_obj.myChildMethodOne()
child_obj.myParentMethOne()
child_obj.newParentMethTwo()
Python

Output:

BigBoxParent __init__
BigBoxChild __init__

My Child Method One
my Parent Method One
new Parent Method Two
Plaintext

Parent __init__ Execution

class BigBoxParent:
    def __init__(self):
        print("BigBoxParent __init__")

    def myParentMethOne(self):
        print("my Parent Method One")
        
    def myParentMethTwo(self):
        print("my Parent Method Two")
        
class NewParent:
    def __init__(self):
        print("NewParent __init__")
        
    def newParentMethOne(self):
        print("new Parent Method one")
        
    def newParentMethTwo(self):
        print("new Parent Method Two")
        
class BigBoxChild(BigBoxParent, NewParent):
    def __init__(self):
        NewParent()
        print("BigBoxChild __init__")
        
    def myChildMethodOne(self):
        print("My Child Method One")
        

child_obj = BigBoxChild()
child_obj.myChildMethodOne()
child_obj.myParentMethOne()
child_obj.newParentMethTwo()
Python

Output:

NewParent __init__
BigBoxChild __init__


My Child Method One
my Parent Method One
new Parent Method Two
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.