1.4 - OOPS Concepts in Python
1.4 - OOPS Concepts in Python
Web Developer-Python 1
Error/ Warning Information Flashback Class Exercise
Web Developer-Python 2
1. Classes and Objects
4. Function Overloading
5. Operator Overloading
Web Developer-Python 3
1. Classes and Objects
Web Developer-Python 4
Object-oriented programming
OOP
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “class” and "object“.
Features:
▪ Classes and Objects
▪ Inheritance and its types
▪ Multiple Inheritance
▪ Function Overloading
▪ Operator Overloading
Web Developer-Python 5
Classes and objects
Class
A class in Python is a blueprint or a template for creating objects.
▪ Classes are created by keyword class.
▪ Attributes are the variables that belong to a class.
▪ Attributes are always public and can be accessed using the dot (.) operator. Example - MyClass.MyAttribute
Object
Class : Human
Name
Human : object01 Human : object02
Job
Address
Web Developer-Python 7
Classes and objects
Create a class
▪ “class” keyword Class : Human
▪ Syntax
▪ Example
Web Developer-Python 8
Classes and objects
Web Developer-Python 9
Classes and objects
Create an object
▪ Syntex Human : tarzan
▪ Example
Web Developer-Python 10
Classes and objects
The isinstance() function is used to check if an object (first argument) is an instance or subclass instance of a
class or a tuple of classes (second argument). It returns True if the object is an instance of the class (or of one of
the classes in the tuple), and False otherwise.
Web Developer-Python 11
Classes and objects
▪ The data associated with the objects or instances are called attributes.
▪ The actions that the objects can perform or allow us to perform on them are called methods.
Web Developer-Python 12
Classes and objects
Web Developer-Python 13
Classes and objects
It does not have to be named “self”, you can call it whatever you like, but it has to be the first parameter of
any function in the class.
Web Developer-Python 14
Classes and objects
Web Developer-Python 15
Classes and objects
Web Developer-Python 16
Classes and objects
Web Developer-Python 17
Classes and objects
Create object “tarzan” of human class and display it’s information using display_info() function.
Web Developer-Python 18
Classes and objects
Web Developer-Python 19
Classes and objects
Delete an object
▪ “del” keyword
▪ Using the “del” keyword delete the “address” property of the “tarzan” object.
Web Developer-Python 20
Classes and objects
Let's create a Car class with the properties - model, brand, fuel_type, and price. And also add two methods: one to
display the car's details and another to calculate the price after applying a discount.
Sample output
Car Details:
Model: Model S
Brand: Tesla
Fuel Type: Electric
Price: $79999
Web Developer-Python 21
Question?
Web Developer-Python 22
2. Inheritance
Web Developer-Python 23
Inheritance
Inheritance in human
Grandparents
Parents
Child 24
Web Developer-Python
Inheritance
Inheritance in python
Inheritance is a mechanism in OOP that allows a class to inherit attributes and methods from another class.
▪ The class that inherits is called the derived class or child class.
▪ The class being inherited from is called the base class or parent class.
Benefits
Web Developer-Python 25
Inheritance
Syntax
Web Developer-Python 26
Inheritance
Types
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Web Developer-Python 27
Inheritance
“self” is a reference to the current instance of the class. It is used to access variables and methods
associated with the current object.
Web Developer-Python 28
Inheritance
1. Single inheritance
A derived class inherits from a single base class.
2. Multiple inheritance
A derived class inherits from more than one base class.
Base class 01: Flyer Base class 02: Swimmer
Web Developer-Python 30
Inheritance
3. Multilevel inheritance
A derived class inherits from another derived class. Level 0
Base class : Animal
Level 1
Derived class : Mammal
Level 2
Derived class : Dog
Web Developer-Python 31
Inheritance
4. Hierarchical inheritance
Multiple derived classes inherit from a single base class.
Base class :
Animal
Web Developer-Python 32
Inheritance
5. Hybrid inheritance
A combination of two or more types of
inheritance.
Animal
Mammal
Swimmer
Fish
Dog
Web Developer-Python 33
Inheritance
Web Developer-Python 34
Inheritance
Web Developer-Python 35
Inheritance
Web Developer-Python 36
Inheritance
Web Developer-Python 37
Inheritance
Disadvantages of inheritance
▪ Increased complexity
▪ Tight coupling
Web Developer-Python 38
Inheritance
The "diamond problem" occurs when a class inherits from two classes that have a common ancestor. This
creates a diamond-shaped inheritance hierarchy, leading to ambiguity in method resolution.
Web Developer-Python 39
Inheritance
Create a Python program to manage different types of vehicles. Follow these steps: # Example Usage
4. CreateWeb
instances of Car and Bike and call their display_info() methods.
Developer-Python 40
Question?
Web Developer-Python 41
4. Function Overloading
Web Developer-Python 42
Function overloading
Definition
Two or more functions have the same name but
▪ different numbers of parameters
▪ or different types of parameters
▪ or both.
This is called function overloading.
Python does not support traditional function overloading. However, a similar effect can be achieved using –
▪ default arguments
▪ variable-length arguments
▪ conditional logic inside methods.
Web Developer-Python 43
Function overloading
Web Developer-Python 44
Function overloading
To perform the same task there are two different methods – speak() and speak_with_sound()
Web Developer-Python 45
Function overloading
Web Developer-Python 46
Function overloading
Web Developer-Python 47
Function overloading
What is inheritance?
Inheritance in OOP allows a new class (called a “child class” or "subclass" or "derived class") is created by inheriting the
properties and behaviors (methods) of an existing class (called a "base class" or "superclass“ or “parent class”).
Web Developer-Python 48
Function overloading
Web Developer-Python 49
Function overloading
▪ Code Reusability: Instead of creating multiple methods with different names, we reuse the same method
name to handle various scenarios.
▪ Flexibility and Ease of Use: The product() method can be called with or without an argument, making it
flexible and easy to use.
Web Developer-Python 51
Function overloading
Implement a class hierarchy with “Person” as the base class and “Student” as # Example Usage
the derived class. The Student class should have a method to add skills. Use
function overloading to handle the following cases: student = Student("John Doe", 20)
student.add_skill("Python")
▪ Objective student.add_skill(["Java", "C++"])
student.add_skill({"Data Analysis": "Advanced",
1. Add a single skill: Takes one string argument (skill) and adds it to the "Machine Learning": "Intermediate"})
student's skill set. print(student.skills)
2. Add multiple skills: Takes a list of strings (skills) and adds them to
the student's skill set. # Expected output
3. Add skills with proficiency levels: Takes a dictionary of skills and
proficiency levels and adds to the skill set. {'Python': 'Beginner’,
'Java': 'Beginner’,
▪ Requirements 'C++': 'Beginner’,
'Data Analysis': 'Advanced’,
• Use the multipledispatch library to achieve function overloading. 'Machine Learning': 'Intermediate'}
• Implement the Person and Student classes.
• Implement the add_skill method in the Student class using function
overloading. 52
Web Developer-Python
Question?
Web Developer-Python 53
5. Operator Overloading
Web Developer-Python 54
Operator overloading
Operator overloading in Python allows to define how operators behave with user-defined objects.
For example, the ‘+’ operator will perform arithmetic addition on two numbers, merge two lists, or concatenate
two strings.
Web Developer-Python 55
Operator overloading
It refers to the ability to define multiple functions with the same name but different type or number of parameters.
Web Developer-Python 56
Operator overloading
Web Developer-Python 57
Operator overloading
▪ Also known as magic methods or dunder methods (because their names are surrounded by double underscores).
▪ These methods allow to define how instances of class behave with operators.
Web Developer-Python 58
Operator overloading
Web Developer-Python 60
Operator overloading
__str__()
This method in Python is a special method used to
define a string representation of an object. It returns
a string whenever the object is printed or converted
to a string using str() function.
# Example Usage
To demonstrate operator overloading using one operator from each category, class Point:
create a custom Point class and overload the following operators: def __init__(self, x=0, y=0):
self.x = x
self.y = y
▪ Arithmetic Operator: + (addition)
▪ Augmented Assignment Operator: += (in-place addition) # Expected Output
▪ Unary Operator: - (negation)
Addition: Point(4, 6)
▪ Comparison Operator: == (equality) In-place Addition: Point(4, 6)
Negation: Point(-4, -6)
▪ Bitwise Operator: & (bitwise AND)
Equality: False
▪ Type Conversion: int() (conversion to integer) Bitwise AND: Point(0, 2)
Integer Conversion: 10
▪ Container/Sequence Operation: [] (indexing)
Indexing: 4 6
▪ Context Management: with statement (context management) Entering context
Within context: Point(7, 8)
Exiting context
Web Developer-Python 62
Question?
Web Developer-Python 63
Thank you
Web Developer-Python 64