Вы находитесь на странице: 1из 4

Polymorphism In Python

Polymorphism defines the ability to take different forms.


Polymorphism in Python allows us to define methods in the child
class with the same name as defined in their parent class.

What is Polymorphism?

Polymorphism is taken from the Greek words Poly (many) and


morphism (forms). It means that the
same function name/operator etc… can be used for different
types. This makes programming more intuitive and easier.

In Python, we have different ways to define polymorphism. 

1. Polymorphism with function objects

Polymorphism with Function Output after the program


and Objects execution
class Capsicum(): You are eating a Vegetable
“A Class of Capsicums” Its color is Green
     def type(self): You are eating a Fruit
       print("You are eating aVegetable") Its color is Yellow
     def color(self):
      print("Its color is Green")
class Mango():
     def type(self):
        print("You are eating a Fruit")
     def color(self):
         print(" Its color is Yellow")
def func(obj):
       obj.type()
       obj.color()
        
obj_Capsicum = Capsicum()
obj_Mango = Mango()
func(obj_Capsicum)
func(obj_Mango)

2. Polymorphism with Class Methods

Polymorphism with Class Methods Output after the


program execution

class India(): India’s Capital is New Delhi


  def capital(self): Our National bird is Peacock
   print("India’s Capital is New Delhi") USA’s capital is Washington, D.C
  Our National bird is Bald Eagle
  def bird(self):
   print(" Our National bird is Peacock")
 
class USA():
     def capital(self):
       print("USA’s capital is Washington, D.C.")
 
     def bird(self):
       print(" Our National bird is Bald Eagle")
 
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
country.capital()
country.language()

3. Polymorphism with Inheritance

This process of re-implementing a method in the child class is


known as Method Overriding

In inheritance, the child class inherits all the attributes/methods


from its parent class.

In some cases Methods in the child class have the same name as
the methods in the parent class.

This is mostly used in cases where the method inherited from the
parent class doesn’t suite the child class….

Polymorphism with Inheritance Output after the program


Method Overriding execution
-----Parent Class----
class Animals: We are a class of Animals
  def details(self): We are a class of Animals
    print(" We are a class of Animals.") We are a class of Animals
 
 def food(self): Some of us are omnivorous who feed
    print(“Some of us are omnivorous who feed on greens on greens and while some are
and while some are carnivorous who feed on the prey”) carnivorous who feed on the prey We
 ----first Child class who inherits the parent class---- being the King of the Forest, love to
class Lion(Animals): hunt our prey
  def food(self): We are considered the most sacred
    print("We being the King of the Forest, love to hunt animals, would love to eat grass and
our prey") greens
 -----Second child class who inherits the parent class------
class Cow(Animals):
  def food(self):
    print(" We are considered the most sacred animals,
would love to eat grass and greens")
 
obj_animals = Animals()
obj_lion = Lion()
obj_cow = Cow()
 
obj_animals.details()
obj_lion.details()
obj_cow.details()
obj_animals.food()
obj_lion.food()
obj_cow.food()

4. Polymorphism with Operators

Polymorphism with P1+p2


Operators (3,4)+(1,2)
Operator Overloading 4,5
class Point: (4,5)
def __init__(self, x = 0, y = 0): __add__(p1,p2)
self.x = x
self.y = y
X=p1.x+p2.x
def __str__(self): Y=p1.y+p2.y
return "({0},{1})".format(self.x,self.y) Return x,y
def __add__(self,other):
x = self.x + other.x
Str(4,5)
y = self.y + other.y
return x,y __str__(4,5)
p1=Point(2,3) (4,5)
p2=Point(-1,2)
p3=Point(0,0)
p3=p1+p2
print ("p1=",p1)
print ("p2=",p2)
print ("p3=",p3)
Output:
p1= (2,3)
p2= (-1,2)
p3= (1, 5)

The print statement and str() built-in function uses __str__ to display the
string representation of the object passed to it

import datetime
today = datetime.datetime.now()
  
# Prints readable format for date-time object
print str(today)
{ Internally the str function calls today.__str__}
2016-02-22 19:32:04.078030