Monday, February 12, 2024

python inheritance sketch

#python inheritance sketch
class Letter(object):
    def __init__(self):
        print("constructor {0} called".format(self.__class__.__name__))
    
    def _fun(self):
        #to be overriten by sub classes
        pass
    
    def doIt(self):
        self._fun()
        
class A(Letter):
    def __init__(self):
        super(A, self).__init__()
    def _fun(self):
        print("fun from {0}".format(self.__class__.__name__))
        
class B(Letter):
    def __init__(self):
        super(B, self).__init__()
    def _fun(self):
        print("fun from {0}".format(self.__class__.__name__))
        
a = A()
b = B()

a.doIt()
b.doIt()

#constructor A called
#constructor B called
#fun from A
#fun from B 
Happy Sketching!