OOP - cont'¶
Inheritance¶
In [1]:
# define a superclass
class super_class:
# attributes and method definition
pass
# inheritance
class sub_class(super_class):
# attributes and method of super_class
# attributes and method of sub_class
pass
In [42]:
class Animal:
# attribute and method of the parent class
name = "" # for educational purposes !!!
def eat(self):
print("I can eat")
class Dog(Animal):
# attribute and method of the child class
def display(self):
print("My name is ", self.name)
dog = Dog() # instance of the class Dog
dog.name = 5 # attribute of the parent class
dog.eat() # method of the parent class
dog.display() # method of the subclass (child) class
I can eat My name is 5
issubclass()
issubclass(subclass,object)
In [3]:
issubclass(Dog, Animal)
Out[3]:
True
In [5]:
issubclass(Animal,Dog)
Out[5]:
False
In [6]:
issubclass(Dog, (Animal,int))
Out[6]:
True
Overriding¶
In [7]:
class Animal:
# attributes and method of the parent class
name = ""
def eat(self):
print("I can eat")
def sleep(self):
print("I can sleep")
# inherit from Animal
class Dog(Animal):
def eat(self):
'''override eat() method'''
print("I like to eat bones")
animal = Animal()
animal.eat()
animal.sleep()
dog = Dog()
dog.eat()
dog.sleep()
I can eat I can sleep I like to eat bones I can sleep
super()
¶
In [9]:
class Animal:
# attributes and method of the parent class
name = ""
def eat(self):
print("I can eat")
def sleep(self):
print("I can sleep")
# inherit from Animal
class Dog(Animal):
def eat(self):
'''override eat() method'''
super().eat()
print("I like to eat bones")
animal = Animal()
animal.eat()
animal.sleep()
print('-' * 10)
dog = Dog()
dog.eat()
dog.sleep()
I can eat I can sleep ---------- I can eat I like to eat bones I can sleep
In [11]:
print(type(animal))
isinstance(animal, Animal)
<class '__main__.Animal'>
Out[11]:
True
In [12]:
isinstance(5, (float,int))
Out[12]:
True
In [14]:
class Person:
def __init__(self, fname = 'John', lname = 'Dow'):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
pass
x = Person()
x.printname()
y = Student() # inhereted init from person
y.printname()
John Dow John Dow
In [19]:
class Person:
def __init__(self, fname = 'John', lname = 'Dow'):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname = 'John', lname = 'Jones', graduationyear = 2019):
super().__init__(fname, lname)
self.graduationyear = graduationyear
def printname(self):
print(self.firstname, self.lastname, self.graduationyear)
def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
x = Person()
x.printname()
y = Student() # inhereted init from person
y.printname()
y.welcome()
John Dow John Jones 2019 Welcome John Jones to the class of 2019
Overloading¶
In [40]:
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return f"({self.x}, {self.y})"
def __len__(self):
return 1
def __add__(self, other):
'''override + operator
input: other - another Point instance
output: Point instance
'''
return Point(self.x + other.x, self.y + other.y)
p1 = Point(1, 2)
p2 = Point(2, 3)
p3 = p1 + p2
print(p1)
print(p3)
print(isinstance(p1,Point))
isinstance(p1+p2,Point)
(1, 2) (3, 5) True
Out[40]:
True
In [29]:
isinstance(Point(),Point)
# Point() - instance
# Point - class
Out[29]:
True
In [31]:
print(Point)
print(Point())
<class '__main__.Point'> (0, 0)
In [34]:
lst = [Point(),Point(1,2)]
for x in lst:
print(x)
(0, 0) (1, 2)
In [37]:
points = []
lst2 = [Point,Point]
for x in lst2:
points.append(x())
[<__main__.Point object at 0x7d0b734e81f0>, <__main__.Point object at 0x7d0b734eab90>]
In [39]:
lst3 = [Point,Point()]
print(lst3)
[<class '__main__.Point'>, <__main__.Point object at 0x7d0ba43ffd90>]
In [41]:
len(p1)
Out[41]:
1