Working and declaring of the inheritance key words in python programme
Python Inheritance Syntax
class BaseClass:
Body of base class
class
DerivedClass(BaseClass): Body of derived class
To demonstrate the use of inheritance, let us take an example.
A polygon is a closed figure with 3 or more sides. Say, we have a class called Polygondefined as follows.
class Polygon:
def __init__(self, no_of_sides):
self.n = no_of_sides self.sides = [0 for i in range(no_of_sides)]
def inputSides(self): self.sides = [float(input("Enter side "+str(i+1)+" : "))
for i in range(self.n)]
def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])
This class has data attributes to store the number of sides, n and magnitude of each side as a list, sides.
Method inputSides() takes in magnitude of each side and similarly, dispSides() will display these properly.