Understanding OOP Concepts in Python
Understanding OOP Concepts in Python
Introduction to Computing
Object-oriented Programming in Python
Malay Bhattacharyya
Associate Professor
2 Features of OOP
Classes and Objects
Encapsulation
Constructor
Destructor
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 Introduction to Computing
Outline Object-oriented Programming (OOP) Features of OOP
Standard class
class PythonProgrammer:
fullname = "Guido van Rossum"
height = 5.6
age = 67
def feature(self):
print("Creator of Python is: " + [Link])
Attributes are always public and can be accessed using the dot
(.) operator.
PP = PythonProgrammer() # Instantiation
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):
[Link] = fullname
[Link] = height
[Link] = age
Constructor
Defining a constructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
[Link] = fullname
[Link] = height
[Link] = age
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
[Link] = fullname
[Link] = height
[Link] = age
def show(self):
print([Link])
PP = PythonProgrammer("Guido van Rossum", 5.6, 67)
[Link]()
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
[Link] = fullname
[Link] = height
[Link] = age
def show(self):
print([Link])
PP = PythonProgrammer("Guido van Rossum", 5.6, 67)
[Link]()
Output:
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname):
[Link] = fullname
def include(self, height):
[Link] = height
def show(self):
print([Link])
print([Link])
PP = PythonProgrammer("Guido van Rossum")
[Link](5.6)
[Link]()
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname):
[Link] = fullname
def include(self, height):
[Link] = height
def show(self):
print([Link])
print([Link])
PP = PythonProgrammer("Guido van Rossum")
[Link](5.6)
[Link]()
Output:
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname):
[Link] = fullname
# We cannot write return([Link]) here
def include(self, height):
[Link] = height
return([Link])
def send(self):
return([Link])
PP = PythonProgrammer("Guido van Rossum")
print([Link]())
print([Link](5.6))
Constructor
Using a constructor:
class PythonProgrammer:
def __init__(self, fullname):
[Link] = fullname
# We cannot write return([Link]) here
def include(self, height):
[Link] = height
return([Link])
def send(self):
return([Link])
PP = PythonProgrammer("Guido van Rossum")
print([Link]())
print([Link](5.6))
Output:
Guido van Rossum
5.6
Malay Bhattacharyya Introduction to Computing
Outline Object-oriented Programming (OOP) Features of OOP
Destructor
Destructors are methods that are useful for any kind of finalization
you want to perform with the objects. Python has a garbage
collector that handles memory management automatically and
efficiently even though you do not use a destructor.
Destructor
Destructors are methods that are useful for any kind of finalization
you want to perform with the objects. Python has a garbage
collector that handles memory management automatically and
efficiently even though you do not use a destructor.
Destructor
Defining a destructor:
class PythonProgrammer:
def __del__(self):
Destructor
Defining a destructor:
class PythonProgrammer:
def __del__(self):
Destructor
Using a destructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
[Link] = fullname
[Link] = height
[Link] = age
def show(self):
print([Link])
def __del__(self):
print("Done")
PP = PythonProgrammer("Guido van Rossum", 5.6, 67)
[Link]()
del PP
Destructor
Using a destructor:
class PythonProgrammer:
def __init__(self, fullname, height, age):
[Link] = fullname
[Link] = height
[Link] = age
def show(self):
print([Link])
def __del__(self):
print("Done")
PP = PythonProgrammer("Guido van Rossum", 5.6, 67)
[Link]()
del PP
Output:
Guido van Rossum
Done
Malay Bhattacharyya Introduction to Computing
Outline Object-oriented Programming (OOP) Features of OOP
Polymorphism
i, j = 2000, 23
print(i + j)
i, j = 2000, 23
print(i + j)
Output:
2023
Bachelor of Statistics
print(len("Python"))
print(len(["ver1", "ver2", "ver3"]))
print(len({"Creator": "Guido", "Nationality": "Dutch"}))
print(len("Python"))
print(len(["ver1", "ver2", "ver3"]))
print(len({"Creator": "Guido", "Nationality": "Dutch"}))
Output:
6
3
2
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):
[Link] = fullname
def show(self):
print([Link] + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
CP = CProgrammer("Dennis Ritchie")
[Link]()
PP = PythonProgrammer("Guido van Rossum")
[Link]()
Single inheritance
class CProgrammer:
def __init__(self, fullname):
[Link] = fullname
def show(self):
print([Link] + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
CP = CProgrammer("Dennis Ritchie")
[Link]()
PP = PythonProgrammer("Guido van Rossum")
[Link]()
Output:
Multi-level inheritance
class CProgrammer:
def __init__(self, fullname):
[Link] = fullname
def show(self):
print([Link] + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
class SmartProgrammer(PythonProgrammer):
pass
SP = SmartProgrammer("Guido van Rossum")
[Link]()
Multi-level inheritance
class CProgrammer:
def __init__(self, fullname):
[Link] = fullname
def show(self):
print([Link] + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
class SmartProgrammer(PythonProgrammer):
pass
SP = SmartProgrammer("Guido van Rossum")
[Link]()
Output:
Hierarchical inheritance
class CProgrammer:
def __init__(self, fullname):
[Link] = fullname
def show(self):
print([Link] + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
class JavaProgrammer(CProgrammer):
pass
PP = PythonProgrammer("Guido van Rossum")
[Link]()
JP = JavaProgrammer("James Gosling")
[Link]()
Hierarchical inheritance
class CProgrammer:
def __init__(self, fullname):
[Link] = fullname
def show(self):
print([Link] + " is a C programmer")
class PythonProgrammer(CProgrammer):
pass
class JavaProgrammer(CProgrammer):
pass
PP = PythonProgrammer("Guido van Rossum")
[Link]()
JP = JavaProgrammer("James Gosling")
[Link]()
Output:
Guido van Rossum is a C programmer
James Gosling is a C programmer
Malay Bhattacharyya Introduction to Computing
Outline Object-oriented Programming (OOP) Features of OOP
Multiple inheritance
class CProgrammer:
def __init__(self, fullname):
[Link] = fullname
def show(self):
print([Link] + " is a C programmer")
class SmartProgrammer():
def credit(self):
print("He is smart!!!")
class PythonProgrammer(CProgrammer, SmartProgrammer):
pass
PP = PythonProgrammer("Guido van Rossum")
[Link]()
[Link]()
Multiple inheritance
class CProgrammer:
def __init__(self, fullname):
[Link] = fullname
def show(self):
print([Link] + " is a C programmer")
class SmartProgrammer():
def credit(self):
print("He is smart!!!")
class PythonProgrammer(CProgrammer, SmartProgrammer):
pass
PP = PythonProgrammer("Guido van Rossum")
[Link]()
[Link]()
Output:
Guido van Rossum is a C programmer
He is smart!!!
Malay Bhattacharyya Introduction to Computing
Outline Object-oriented Programming (OOP) Features of OOP
Output:
Checking inheritance
Checking inheritance
class CProgrammer:
def __init__(self, fullname):
[Link] = fullname
def show(self):
print([Link] + " 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):
[Link] = fullname
def show(self):
print([Link] + " 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):
[Link] = fullname
def show(self):
print([Link] + " 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):
[Link] = fullname
def show(self):
print([Link] + " 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):
[Link] = fullname
def __repr__(self):
return ’Items: {}’.format([Link]())
def show(self):
print([Link])
PP = PythonProgrammer("Guido van Rossum")
print(PP)
[Link]()
class PythonProgrammer:
def __init__(self, fullname):
[Link] = fullname
def __repr__(self):
return ’Items: {}’.format([Link]())
def show(self):
print([Link])
PP = PythonProgrammer("Guido van Rossum")
print(PP)
[Link]()