SlideShare a Scribd company logo
2
www.datacademy.ai
Knowledge world
class Lizard():
def animal_kingdom(self):
print(“Mammal”)
def legs(self):
print(“Four”)
def function1(obj):
obj.animal_kingdom()
obj.legs()
obj_dog = Dog()
obj_lizard = Lizard()
function1(obj_dog)
function1(obj_lizard)
Output
Mammal
Four
Mammal
Four
In the above example, the function, function1() takes in an object named obj,
which in turn lets the functions call the methods, animal_kingdom() and legs()
of both the classes, Dog and Lizard. To do this, we must create the instances of
both classes.
Polymorphism With Class Methods
Most read
8
www.datacademy.ai
Knowledge world
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethoddef area(self):
passclass Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side * self.side
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * (self.radius ** 2)
def get_shape_area(shape: Shape):
print(shape.area())
square = Square(4)
circle = Circle(5)
Most read
9
www.datacademy.ai
Knowledge world
get_shape_area(square) #16
get_shape_area(circle)
In this example, the get_shape_area function can take any object that is an
instance of the Shape class or any class that inherits from it, regardless of their
specific implementation of the area() method, and it will correctly call
the area() method for that class.
Most read
www.datacademy.ai
Knowledge world
Learn Polymorphism in Python with Examples
Main Features Of Python include:
• Beginner-friendly Language – Python is easy to learn, maintain,
implement and read. It is interactive in nature.
• Object-Oriented – Python encapsulates code within objects by
supporting the Object-Oriented programming approach or style or
approach.
• Industry-oriented – Python is extendable, portable, scalable, cross-
platform friendly with a standard library, and has support for GUI
applications and interactive mode.
What is Polymorphism in Python?
In Python, polymorphisms refer to the occurrence of something in multiple
forms. As part of polymorphism, a Python child class has methods with the
same name as a parent class method. This is an essential part of
programming. A single type of entity is used to represent a variety of types in
different contexts (methods, operators, objects, etc.)
Polymorphism may be used in Python in various ways. Polymorphism can be
defined using numerous functions, class methods, and objects. So, let us deep-
dive into each of these ways.
Polymorphism With Function and Objects
To induce polymorphism using a function, we need to create such a function
that can take any object. Here is an example of Polymorphism using functions
and objects:
Code
class Dog():
def animal_kingdom(self):
print(“Mammal”)
def legs(self):
print(“Four”)
www.datacademy.ai
Knowledge world
class Lizard():
def animal_kingdom(self):
print(“Mammal”)
def legs(self):
print(“Four”)
def function1(obj):
obj.animal_kingdom()
obj.legs()
obj_dog = Dog()
obj_lizard = Lizard()
function1(obj_dog)
function1(obj_lizard)
Output
Mammal
Four
Mammal
Four
In the above example, the function, function1() takes in an object named obj,
which in turn lets the functions call the methods, animal_kingdom() and legs()
of both the classes, Dog and Lizard. To do this, we must create the instances of
both classes.
Polymorphism With Class Methods
www.datacademy.ai
Knowledge world
Let us discuss how to implement Polymorphism in Python using Class
Methods.
In the same way, Python makes use of two separate class types. For this, we
design a for loop that iterates through an item tuple. After that, we need to
perform method calling without regard for the class type of each object. We
take it for granted that these methods have their existence in each of the
classes.
A perfect example depicting Polymorphism with Class Methods using Python:
Code
class Car():
def wheels(self):
print(4)
def mode_of_transport(self):
print(“Private usually”)
class Bus():
def wheels(self):
print(8)
def mode_of_transport(self):
print(“Public usually”)
obj_car = Car()
obj_bus = Bus()
for vehicle in (obj_car, obj_bus):
vehicle.wheels()
www.datacademy.ai
Knowledge world
vehicle.mode_of_transport()
Output
4
Private usually
8
Public usually
In the above example, class methods wheels(), mode_of_transport(), which
belong to Car and Bus classes, are directly invoked using the instances of these
two classes in the for loop, which loops through both the class methods.
Polymorphism With Inheritance
Polymorphism, a child class method is allowed to have the same name as the
class methods in the parent class. In inheritance, the methods belonging to the
parent class are passed down to the child class. It’s also possible to change a
method that a child class has inherited from its parent.
This is typically used whenever a parent class inherited method is not
appropriate for the child class. To rectify this situation, we use Method
Overriding, which enables re-implementing of a method in a child class.
What Is Method Overriding?
Method overriding is an object-oriented programming technique that lets us
change the function implementation of a parent class in the child class.
Method overriding is basically a child class’s ability to implement change in
any method given by one of its parent classes.
Conditions of Method Overriding:
• There should be inheritance. Within a class, function overriding is not
possible, therefore a child class is derived from a parent class.
• A function of the child class should have the same number of parameters
as that of the parent class.
www.datacademy.ai
Knowledge world
We know that in inheritance, a child class gets access to the protected and
public methods and variables of the parent class whenever it inherits it. We
exploit this concept to implement Polymorphism using Inheritance as well.
A perfect example of Method Overriding and Polymorphism with Inheritance
is:
Code
class Vehicle:
def desc(self):
print(“So many categories of vehicle”)
def wheels(self):
print(“Differs according to the vehicle category”)
class car(Vehicle):
def wheels(self):
print(4)
class bus(Vehicle):
def wheel(self):
print(8)
obj_vehicle = Vehicle()
obj_car = car()
obj_bus = bus()
obj_vehicle.desc()
obj_vehicle.wheels()
www.datacademy.ai
Knowledge world
obj_car.desc()
obj_car.wheels()
obj_bus.desc()
obj_bus.wheels()
Output
So many categories of vehicle
Differs according to the vehicle category
So many categories of vehicle
4
So many categories of vehicle
Differs according to the vehicle category
Polymorphism is a concept in object-oriented programming where a single
function or method can operate on multiple types of objects. In Python,
polymorphism can be achieved through the use of inheritance and interfaces.
Here is an example of polymorphism using inheritance:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
www.datacademy.ai
Knowledge world
class Dog(Animal):
def speak(self):
return "Woof"class Cat(Animal):
def speak(self):
return "Meow"def animal_speak(animal):
print(animal.speak())
dog = Dog("Fido")
cat = Cat("Whiskers")
animal_speak(dog) # "Woof"
animal_speak(cat) # "Me
In this example, the animal_speak function can take an object of any class
that inherits from the Animal class, and it will correctly call
the speak() method for that class.
You can also achieve polymorphism through the use of interfaces or abstract
base classes. Here’s an example:
www.datacademy.ai
Knowledge world
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethoddef area(self):
passclass Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side * self.side
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * (self.radius ** 2)
def get_shape_area(shape: Shape):
print(shape.area())
square = Square(4)
circle = Circle(5)
www.datacademy.ai
Knowledge world
get_shape_area(square) #16
get_shape_area(circle)
In this example, the get_shape_area function can take any object that is an
instance of the Shape class or any class that inherits from it, regardless of their
specific implementation of the area() method, and it will correctly call
the area() method for that class.

More Related Content

Similar to Learn Polymorphism in Python with Examples.pdf (20)

Chapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptxChapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptx
fikadumeuedu
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
OOPS 46 slide Python concepts .pptx
OOPS 46 slide Python concepts       .pptxOOPS 46 slide Python concepts       .pptx
OOPS 46 slide Python concepts .pptx
mrsam3062
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
Ahmed Farag
 
PythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingPythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programming
KhadijaKhadijaAouadi
 
What is java polymorphism and its types in java training?
What is  java polymorphism and its types in java training?What is  java polymorphism and its types in java training?
What is java polymorphism and its types in java training?
kritikumar16
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphism
TOPS Technologies
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
AnmolVerma363503
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptx
ShuvrojitMajumder
 
09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching
quanhoangd129
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Java OOPs Concepts.docx
Java OOPs Concepts.docxJava OOPs Concepts.docx
Java OOPs Concepts.docx
FredWauyo
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
AshutoshTrivedi30
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Ahmed Za'anin
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
Basics of oops concept
Basics of oops conceptBasics of oops concept
Basics of oops concept
DINESH KUMAR ARIVARASAN
 
OOP- PolymorphismFinal12injavait101.pptx
OOP- PolymorphismFinal12injavait101.pptxOOP- PolymorphismFinal12injavait101.pptx
OOP- PolymorphismFinal12injavait101.pptx
codevincent624
 
C++ first s lide
C++ first s lideC++ first s lide
C++ first s lide
Sudhriti Gupta
 
Chapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptxChapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptx
fikadumeuedu
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
OOPS 46 slide Python concepts .pptx
OOPS 46 slide Python concepts       .pptxOOPS 46 slide Python concepts       .pptx
OOPS 46 slide Python concepts .pptx
mrsam3062
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
Ahmed Farag
 
PythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingPythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programming
KhadijaKhadijaAouadi
 
What is java polymorphism and its types in java training?
What is  java polymorphism and its types in java training?What is  java polymorphism and its types in java training?
What is java polymorphism and its types in java training?
kritikumar16
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphism
TOPS Technologies
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptx
ShuvrojitMajumder
 
09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching
quanhoangd129
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Java OOPs Concepts.docx
Java OOPs Concepts.docxJava OOPs Concepts.docx
Java OOPs Concepts.docx
FredWauyo
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
OOP- PolymorphismFinal12injavait101.pptx
OOP- PolymorphismFinal12injavait101.pptxOOP- PolymorphismFinal12injavait101.pptx
OOP- PolymorphismFinal12injavait101.pptx
codevincent624
 

More from Datacademy.ai (16)

Characteristics of Big Data Understanding the Five V.pdf
Characteristics of Big Data  Understanding the Five V.pdfCharacteristics of Big Data  Understanding the Five V.pdf
Characteristics of Big Data Understanding the Five V.pdf
Datacademy.ai
 
Why Monitoring and Logging are Important in DevOps.pdf
Why Monitoring and Logging are Important in DevOps.pdfWhy Monitoring and Logging are Important in DevOps.pdf
Why Monitoring and Logging are Important in DevOps.pdf
Datacademy.ai
 
AWS data storage Amazon S3, Amazon RDS.pdf
AWS data storage Amazon S3, Amazon RDS.pdfAWS data storage Amazon S3, Amazon RDS.pdf
AWS data storage Amazon S3, Amazon RDS.pdf
Datacademy.ai
 
Top 30+ Latest AWS Certification Interview Questions on AWS BI and data visua...
Top 30+ Latest AWS Certification Interview Questions on AWS BI and data visua...Top 30+ Latest AWS Certification Interview Questions on AWS BI and data visua...
Top 30+ Latest AWS Certification Interview Questions on AWS BI and data visua...
Datacademy.ai
 
Top 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdfTop 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdf
Datacademy.ai
 
Interview Questions on AWS Elastic Compute Cloud (EC2).pdf
Interview Questions on AWS Elastic Compute Cloud (EC2).pdfInterview Questions on AWS Elastic Compute Cloud (EC2).pdf
Interview Questions on AWS Elastic Compute Cloud (EC2).pdf
Datacademy.ai
 
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
Datacademy.ai
 
Top 30+ Latest AWS Certification Interview Questions on AWS BI & Data Visuali...
Top 30+ Latest AWS Certification Interview Questions on AWS BI & Data Visuali...Top 30+ Latest AWS Certification Interview Questions on AWS BI & Data Visuali...
Top 30+ Latest AWS Certification Interview Questions on AWS BI & Data Visuali...
Datacademy.ai
 
Top 60 Power BI Interview Questions and Answers for 2023.pdf
Top 60 Power BI Interview Questions and Answers for 2023.pdfTop 60 Power BI Interview Questions and Answers for 2023.pdf
Top 60 Power BI Interview Questions and Answers for 2023.pdf
Datacademy.ai
 
Top 100+ Google Data Science Interview Questions.pdf
Top 100+ Google Data Science Interview Questions.pdfTop 100+ Google Data Science Interview Questions.pdf
Top 100+ Google Data Science Interview Questions.pdf
Datacademy.ai
 
AWS DevOps: Introduction to DevOps on AWS
  AWS DevOps: Introduction to DevOps on AWS  AWS DevOps: Introduction to DevOps on AWS
AWS DevOps: Introduction to DevOps on AWS
Datacademy.ai
 
Data Engineering.pdf
Data Engineering.pdfData Engineering.pdf
Data Engineering.pdf
Datacademy.ai
 
Top 140+ Advanced SAS Interview Questions and Answers.pdf
Top 140+ Advanced SAS Interview Questions and Answers.pdfTop 140+ Advanced SAS Interview Questions and Answers.pdf
Top 140+ Advanced SAS Interview Questions and Answers.pdf
Datacademy.ai
 
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
Datacademy.ai
 
Top 60+ Data Warehouse Interview Questions and Answers.pdf
Top 60+ Data Warehouse Interview Questions and Answers.pdfTop 60+ Data Warehouse Interview Questions and Answers.pdf
Top 60+ Data Warehouse Interview Questions and Answers.pdf
Datacademy.ai
 
Top Most Python Interview Questions.pdf
Top Most Python Interview Questions.pdfTop Most Python Interview Questions.pdf
Top Most Python Interview Questions.pdf
Datacademy.ai
 
Characteristics of Big Data Understanding the Five V.pdf
Characteristics of Big Data  Understanding the Five V.pdfCharacteristics of Big Data  Understanding the Five V.pdf
Characteristics of Big Data Understanding the Five V.pdf
Datacademy.ai
 
Why Monitoring and Logging are Important in DevOps.pdf
Why Monitoring and Logging are Important in DevOps.pdfWhy Monitoring and Logging are Important in DevOps.pdf
Why Monitoring and Logging are Important in DevOps.pdf
Datacademy.ai
 
AWS data storage Amazon S3, Amazon RDS.pdf
AWS data storage Amazon S3, Amazon RDS.pdfAWS data storage Amazon S3, Amazon RDS.pdf
AWS data storage Amazon S3, Amazon RDS.pdf
Datacademy.ai
 
Top 30+ Latest AWS Certification Interview Questions on AWS BI and data visua...
Top 30+ Latest AWS Certification Interview Questions on AWS BI and data visua...Top 30+ Latest AWS Certification Interview Questions on AWS BI and data visua...
Top 30+ Latest AWS Certification Interview Questions on AWS BI and data visua...
Datacademy.ai
 
Top 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdfTop 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdf
Datacademy.ai
 
Interview Questions on AWS Elastic Compute Cloud (EC2).pdf
Interview Questions on AWS Elastic Compute Cloud (EC2).pdfInterview Questions on AWS Elastic Compute Cloud (EC2).pdf
Interview Questions on AWS Elastic Compute Cloud (EC2).pdf
Datacademy.ai
 
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
Datacademy.ai
 
Top 30+ Latest AWS Certification Interview Questions on AWS BI & Data Visuali...
Top 30+ Latest AWS Certification Interview Questions on AWS BI & Data Visuali...Top 30+ Latest AWS Certification Interview Questions on AWS BI & Data Visuali...
Top 30+ Latest AWS Certification Interview Questions on AWS BI & Data Visuali...
Datacademy.ai
 
Top 60 Power BI Interview Questions and Answers for 2023.pdf
Top 60 Power BI Interview Questions and Answers for 2023.pdfTop 60 Power BI Interview Questions and Answers for 2023.pdf
Top 60 Power BI Interview Questions and Answers for 2023.pdf
Datacademy.ai
 
Top 100+ Google Data Science Interview Questions.pdf
Top 100+ Google Data Science Interview Questions.pdfTop 100+ Google Data Science Interview Questions.pdf
Top 100+ Google Data Science Interview Questions.pdf
Datacademy.ai
 
AWS DevOps: Introduction to DevOps on AWS
  AWS DevOps: Introduction to DevOps on AWS  AWS DevOps: Introduction to DevOps on AWS
AWS DevOps: Introduction to DevOps on AWS
Datacademy.ai
 
Data Engineering.pdf
Data Engineering.pdfData Engineering.pdf
Data Engineering.pdf
Datacademy.ai
 
Top 140+ Advanced SAS Interview Questions and Answers.pdf
Top 140+ Advanced SAS Interview Questions and Answers.pdfTop 140+ Advanced SAS Interview Questions and Answers.pdf
Top 140+ Advanced SAS Interview Questions and Answers.pdf
Datacademy.ai
 
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
50 Extraordinary AWS CloudWatch Interview Questions & Answers.pdf
Datacademy.ai
 
Top 60+ Data Warehouse Interview Questions and Answers.pdf
Top 60+ Data Warehouse Interview Questions and Answers.pdfTop 60+ Data Warehouse Interview Questions and Answers.pdf
Top 60+ Data Warehouse Interview Questions and Answers.pdf
Datacademy.ai
 
Top Most Python Interview Questions.pdf
Top Most Python Interview Questions.pdfTop Most Python Interview Questions.pdf
Top Most Python Interview Questions.pdf
Datacademy.ai
 
Ad

Recently uploaded (20)

Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Ad

Learn Polymorphism in Python with Examples.pdf

  • 1. www.datacademy.ai Knowledge world Learn Polymorphism in Python with Examples Main Features Of Python include: • Beginner-friendly Language – Python is easy to learn, maintain, implement and read. It is interactive in nature. • Object-Oriented – Python encapsulates code within objects by supporting the Object-Oriented programming approach or style or approach. • Industry-oriented – Python is extendable, portable, scalable, cross- platform friendly with a standard library, and has support for GUI applications and interactive mode. What is Polymorphism in Python? In Python, polymorphisms refer to the occurrence of something in multiple forms. As part of polymorphism, a Python child class has methods with the same name as a parent class method. This is an essential part of programming. A single type of entity is used to represent a variety of types in different contexts (methods, operators, objects, etc.) Polymorphism may be used in Python in various ways. Polymorphism can be defined using numerous functions, class methods, and objects. So, let us deep- dive into each of these ways. Polymorphism With Function and Objects To induce polymorphism using a function, we need to create such a function that can take any object. Here is an example of Polymorphism using functions and objects: Code class Dog(): def animal_kingdom(self): print(“Mammal”) def legs(self): print(“Four”)
  • 2. www.datacademy.ai Knowledge world class Lizard(): def animal_kingdom(self): print(“Mammal”) def legs(self): print(“Four”) def function1(obj): obj.animal_kingdom() obj.legs() obj_dog = Dog() obj_lizard = Lizard() function1(obj_dog) function1(obj_lizard) Output Mammal Four Mammal Four In the above example, the function, function1() takes in an object named obj, which in turn lets the functions call the methods, animal_kingdom() and legs() of both the classes, Dog and Lizard. To do this, we must create the instances of both classes. Polymorphism With Class Methods
  • 3. www.datacademy.ai Knowledge world Let us discuss how to implement Polymorphism in Python using Class Methods. In the same way, Python makes use of two separate class types. For this, we design a for loop that iterates through an item tuple. After that, we need to perform method calling without regard for the class type of each object. We take it for granted that these methods have their existence in each of the classes. A perfect example depicting Polymorphism with Class Methods using Python: Code class Car(): def wheels(self): print(4) def mode_of_transport(self): print(“Private usually”) class Bus(): def wheels(self): print(8) def mode_of_transport(self): print(“Public usually”) obj_car = Car() obj_bus = Bus() for vehicle in (obj_car, obj_bus): vehicle.wheels()
  • 4. www.datacademy.ai Knowledge world vehicle.mode_of_transport() Output 4 Private usually 8 Public usually In the above example, class methods wheels(), mode_of_transport(), which belong to Car and Bus classes, are directly invoked using the instances of these two classes in the for loop, which loops through both the class methods. Polymorphism With Inheritance Polymorphism, a child class method is allowed to have the same name as the class methods in the parent class. In inheritance, the methods belonging to the parent class are passed down to the child class. It’s also possible to change a method that a child class has inherited from its parent. This is typically used whenever a parent class inherited method is not appropriate for the child class. To rectify this situation, we use Method Overriding, which enables re-implementing of a method in a child class. What Is Method Overriding? Method overriding is an object-oriented programming technique that lets us change the function implementation of a parent class in the child class. Method overriding is basically a child class’s ability to implement change in any method given by one of its parent classes. Conditions of Method Overriding: • There should be inheritance. Within a class, function overriding is not possible, therefore a child class is derived from a parent class. • A function of the child class should have the same number of parameters as that of the parent class.
  • 5. www.datacademy.ai Knowledge world We know that in inheritance, a child class gets access to the protected and public methods and variables of the parent class whenever it inherits it. We exploit this concept to implement Polymorphism using Inheritance as well. A perfect example of Method Overriding and Polymorphism with Inheritance is: Code class Vehicle: def desc(self): print(“So many categories of vehicle”) def wheels(self): print(“Differs according to the vehicle category”) class car(Vehicle): def wheels(self): print(4) class bus(Vehicle): def wheel(self): print(8) obj_vehicle = Vehicle() obj_car = car() obj_bus = bus() obj_vehicle.desc() obj_vehicle.wheels()
  • 6. www.datacademy.ai Knowledge world obj_car.desc() obj_car.wheels() obj_bus.desc() obj_bus.wheels() Output So many categories of vehicle Differs according to the vehicle category So many categories of vehicle 4 So many categories of vehicle Differs according to the vehicle category Polymorphism is a concept in object-oriented programming where a single function or method can operate on multiple types of objects. In Python, polymorphism can be achieved through the use of inheritance and interfaces. Here is an example of polymorphism using inheritance: class Animal: def __init__(self, name): self.name = name def speak(self): pass
  • 7. www.datacademy.ai Knowledge world class Dog(Animal): def speak(self): return "Woof"class Cat(Animal): def speak(self): return "Meow"def animal_speak(animal): print(animal.speak()) dog = Dog("Fido") cat = Cat("Whiskers") animal_speak(dog) # "Woof" animal_speak(cat) # "Me In this example, the animal_speak function can take an object of any class that inherits from the Animal class, and it will correctly call the speak() method for that class. You can also achieve polymorphism through the use of interfaces or abstract base classes. Here’s an example:
  • 8. www.datacademy.ai Knowledge world from abc import ABC, abstractmethod class Shape(ABC): @abstractmethoddef area(self): passclass Square(Shape): def __init__(self, side): self.side = side def area(self): return self.side * self.side class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * (self.radius ** 2) def get_shape_area(shape: Shape): print(shape.area()) square = Square(4) circle = Circle(5)
  • 9. www.datacademy.ai Knowledge world get_shape_area(square) #16 get_shape_area(circle) In this example, the get_shape_area function can take any object that is an instance of the Shape class or any class that inherits from it, regardless of their specific implementation of the area() method, and it will correctly call the area() method for that class.