HH IIMC PythonBasics s2
HH IIMC PythonBasics s2
class Rectangle():
def __init__(self,l,b):
self.length = l
self.breadth = b
def getArea(self):
return self.length*self.breadth
def getPerimeter(self):
return 2*(self.length+self.breadth)
a = Rectangle(2,4)
print a.getArea()
Subclass of a class
class Child():
def __init__(self,name):
self.name = name
class Student(Child):
def __init__(self,name,roll):
self.roll = roll
Child.__init__(self,name)
a = Child("xyz")
print a.name
b = Student("abc",12)
print b.name
print b.roll
If we wouldn't have an initializer in the Student class, it would have used its Child's initializer and
we would have made objects by just passing the 'name'(a parameter). An example is given below
class Child():
def __init__(self,name):
self.name = name
class Student(Child):
pass
a = Child("xyz")
print a.name
b = Student("abc")
print b.name