#!/usr/bin/env python # coding: utf-8 # # Inheritance # - powerful feature that facilitates code reuse mimicking real-world phenomena # - ability to define a new class (child) that is modified version of an existing class (parent) # - can add new methods and properties to a class without modifying the existing class # Limitation(s):
# - can make programs difficult to read # - when method is invoked, it is sometimes not clear where to find its definition esp. in a large project relevant code may be scattered among several modules # In[10]: class A(object): def __init__(self): self.a = 'A' def printMe(self): print("A's info: {}".format(self.a)) class B: def __init__(self): self.b = 'B' def printMe(self): print("B's info: {}".format(self.b)) class C(B, A): # what happens if you change order? def __init__(self): A.__init__(self) B.__init__(self) self.c = 'C' def printMe(self): print("A's info: {}".format(self.a)) print("B's info: {}".format(self.b)) print("C's info: {}".format(self.c)) # In[11]: obja = A() obja.printMe() objb = B() objb.printMe() objc = C() objc.printMe() # In[ ]: # How can objects of type C can print their c property info # ## abc module - Abstract Base Classes # - allows to define ABCs with abstract methods @abstractmethod decorators # In[ ]: