2020 Day10 Oop Python
2020 Day10 Oop Python
2020 Day10 Oop Python
Computing Laboratory
Object-oriented Programming with Python
Malay Bhattacharyya
Assistant Professor
2 Features of OOP
Classes and Objects
Encapsulation
Constructor
Polymorphism
Inheritance
Special features
A class contains:
Data members known as attributes of the class
Member functions known as methods of the class
A class contains:
Data members known as attributes of the class
Member functions known as methods of the class
Empty class
class PythonProgrammer:
pass
PP = PythonProgrammer() # Instantiation
PP = PythonProgrammer() # Instantiation
class PythonProgrammer:
pass
PP = PythonProgrammer()
print(PP)
PP = PythonProgrammer() # Instantiation
class PythonProgrammer:
pass
PP = PythonProgrammer()
print(PP)
Output:
<__main__.PythonProgrammer object at 0x7f3d6b7f4b70>
Note: Though PP1 and PP2 are instances of the same class, they
represent two distinct objects in memory.
Malay Bhattacharyya Computing Laboratory
Outline Object-oriented Programming (OOP) Features of OOP
class PythonProgrammer:
pass
PP1 = PythonProgrammer()
PP1.fullname = "Malay Bhattacharyya"
PP1.height = 5.6
PP1.age = 38
PP2 = PythonProgrammer()
PP2.fullname = "Mandar Mitra"
PP2.height = 5.7
PP2.age = 49
print("Average age:", (PP1.age + PP2.age) / 2)
class PythonProgrammer:
pass
PP1 = PythonProgrammer()
PP1.fullname = "Malay Bhattacharyya"
PP1.height = 5.6
PP1.age = 38
PP2 = PythonProgrammer()
PP2.fullname = "Mandar Mitra"
PP2.height = 5.7
PP2.age = 49
print("Average age:", (PP1.age + PP2.age) / 2)
Standard class
class PythonProgrammer:
fullname = "Guido van Rossum"
height = 5.66
age = 64
def feature(self):
print("Creator of Python is: " + self.fullname)
Attributes are always public and can be accessed using the dot
(.) operator.
PP = PythonProgrammer() # Instantiation
class PythonProgrammer:
fullname = "Guido van Rossum"
height = 5.66
age = 64
def feature(self):
print("Creator of Python is: " + self.fullname)
PP = PythonProgrammer()
PP.feature() # Method via object
print("Height: " + str(PP.height)) # Attribute via object
print("Age: " + str(PP.age)) # Attribute via object
class PythonProgrammer:
fullname = "Guido van Rossum"
height = 5.66
age = 64
def feature(self):
print("Creator of Python is: " + self.fullname)
PP = PythonProgrammer()
PP.feature() # Method via object
print("Height: " + str(PP.height)) # Attribute via object
print("Age: " + str(PP.age)) # Attribute via object
Objects of built-in types like int, float, bool, str, tuple, unicode are
immutable in Python.
Objects of built-in types like int, float, bool, str, tuple, unicode are
immutable in Python.
Encapsulation
Constructor
Constructor
Constructor
Defining a constructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
self.fullname = fullname
self.height = height
self.age = age
Constructor
Defining a constructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
self.fullname = fullname
self.height = height
self.age = age
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
self.fullname = fullname
self.height = height
self.age = age
def show(self):
print(self.fullname)
PP = PythonProgrammer("Guido van Rossum", 5.66, 64)
PP.show()
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
self.fullname = fullname
self.height = height
self.age = age
def show(self):
print(self.fullname)
PP = PythonProgrammer("Guido van Rossum", 5.66, 64)
PP.show()
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def include(self, height):
self.height = height
def show(self):
print(self.fullname)
print(self.height)
PP = PythonProgrammer("Guido van Rossum")
PP.include(5.66)
PP.show()
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def include(self, height):
self.height = height
def show(self):
print(self.fullname)
print(self.height)
PP = PythonProgrammer("Guido van Rossum")
PP.include(5.66)
PP.show()
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname):
self.fullname = fullname
# We cannot write return(self.fullname) here
def include(self, height):
self.height = height
return(self.height)
def send(self):
return(self.fullname)
PP = PythonProgrammer("Guido van Rossum")
print(PP.send())
print(PP.include(5.66))
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname):
self.fullname = fullname
# We cannot write return(self.fullname) here
def include(self, height):
self.height = height
return(self.height)
def send(self):
return(self.fullname)
PP = PythonProgrammer("Guido van Rossum")
print(PP.send())
print(PP.include(5.66))
Output: Guido van Rossum
5.66
Malay Bhattacharyya Computing Laboratory
Outline Object-oriented Programming (OOP) Features of OOP
Polymorphism
Polymorphism
i, j = 2000, 20
print(i + j)
s1 = "Computing"
s2 = "Lab"
print(s1 + " " + s2)
Polymorphism
i, j = 2000, 20
print(i + j)
s1 = "Computing"
s2 = "Lab"
print(s1 + " " + s2)
Output: 2020
Computing Lab
Polymorphism
print(len("Python"))
print(len(["ver1", "ver2", "ver3"]))
print(len({"Creator": "Guido", "Nationality": "Dutch"}))
Polymorphism
print(len("Python"))
print(len(["ver1", "ver2", "ver3"]))
print(len({"Creator": "Guido", "Nationality": "Dutch"}))
Output: 6
3
2
Polymorphism
Example of method overloading:
class PythonProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print("Creator of Python is: " + self.fullname)
class JavaProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print("Creator of Java is: " + self.fullname)
PP = PythonProgrammer("Guido van Rossum")
PP.show()
JP = JavaProgrammer("James Gosling")
JP.show()
Malay Bhattacharyya Computing Laboratory
Outline Object-oriented Programming (OOP) Features of OOP
Inheritance
Inheritance
Inheritance
Inheritance
Inheritance
Inheritance
Inheritance
The syntax:
class SuperClass:
Attributes of SuperClass
Methods of SuperClass
class SubClass(SuperClass):
Attributes of SubClass
Methods of SubClass
Single inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
CP = CProgrammer("Dennis Ritchie")
CP.show()
PP = PythonProgrammer("Guido van Rossum")
PP.show()
Single inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
CP = CProgrammer("Dennis Ritchie")
CP.show()
PP = PythonProgrammer("Guido van Rossum")
PP.show()
Multi-level inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
class SmartProgrammer(PythonProgrammer):
pass
SP = SmartProgrammer("Guido van Rossum")
SP.show()
Multi-level inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
class SmartProgrammer(PythonProgrammer):
pass
SP = SmartProgrammer("Guido van Rossum")
SP.show()
Hierarchical inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
class JavaProgrammer(CProgrammer):
pass
PP = PythonProgrammer("Guido van Rossum")
PP.show()
JP = JavaProgrammer("James Gosling")
JP.show()
Hierarchical inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
class JavaProgrammer(CProgrammer):
pass
PP = PythonProgrammer("Guido van Rossum")
PP.show()
JP = JavaProgrammer("James Gosling")
JP.show()
Output: Guido van Rossum is a C programmer
James Gosling is a C programmer
Malay Bhattacharyya Computing Laboratory
Outline Object-oriented Programming (OOP) Features of OOP
Multiple inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class SmartProgrammer():
def credit(self):
print("He is smart!!!")
class PythonProgrammer(CProgrammer, SmartProgrammer):
pass
PP = PythonProgrammer("Guido van Rossum")
PP.show()
PP.credit()
Multiple inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class SmartProgrammer():
def credit(self):
print("He is smart!!!")
class PythonProgrammer(CProgrammer, SmartProgrammer):
pass
PP = PythonProgrammer("Guido van Rossum")
PP.show()
PP.credit()
Output: Guido van Rossum is a C programmer
He is smart!!!
Malay Bhattacharyya Computing Laboratory
Outline Object-oriented Programming (OOP) Features of OOP
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
def show(self):
print(self.fullname + " is a Python programmer")
PP = PythonProgrammer("Guido van Rossum")
PP.show()
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
def show(self):
print(self.fullname + " is a Python programmer")
PP = PythonProgrammer("Guido van Rossum")
PP.show()
Checking inheritance
Checking inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
CP = CProgrammer("Dennis Ritchie")
PP = PythonProgrammer("Guido van Rossum")
print(isinstance(CP, PythonProgrammer))
print(isinstance(PP, CProgrammer))
Checking inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
CP = CProgrammer("Dennis Ritchie")
PP = PythonProgrammer("Guido van Rossum")
print(isinstance(CP, PythonProgrammer))
print(isinstance(PP, CProgrammer))
Output: False
True
Checking inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
CP = CProgrammer("Dennis Ritchie")
PP = PythonProgrammer("Guido van Rossum")
print(issubclass(CProgrammer, PythonProgrammer))
print(issubclass(PythonProgrammer, CProgrammer))
Checking inheritance
class CProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def show(self):
print(self.fullname + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
CP = CProgrammer("Dennis Ritchie")
PP = PythonProgrammer("Guido van Rossum")
print(issubclass(CProgrammer, PythonProgrammer))
print(issubclass(PythonProgrammer, CProgrammer))
Output: False
True
class PythonProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def __repr__(self):
return ’Items: {}’.format(self.fullname.split())
def show(self):
print(self.fullname)
PP = PythonProgrammer("Guido van Rossum")
print(PP)
PP.show()
class PythonProgrammer:
def __init__(self, fullname):
self.fullname = fullname
def __repr__(self):
return ’Items: {}’.format(self.fullname.split())
def show(self):
print(self.fullname)
PP = PythonProgrammer("Guido van Rossum")
print(PP)
PP.show()