what is the practical use of polymorphism in Python

+2 votes
I understand the concept of polymorphism but how can I apply it while coding? Can anyone tell me the practical usage of it.
Mar 31, 2018 in Python by ana1504.k
• 7,910 points
4,808 views

3 answers to this question.

+2 votes
Best answer

Polymorphism is the ability to present the same interface for differing underlying forms. In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A, it can do some of the things that class A does differently. It is most commonly used while dealing with inheritance. Python is implicitly polymorphic, it has the ability to overload standard operators so that they have appropriate behaviour based on their context.

Refer the below example:

class Animal:

    def __init__(self,name):

        self.name=name

        def talk(self):

            pass

class Dog(Animal):

            def talk(self):

                print('Woof')

class Cat(Animal):

    def talk(self):

        print('MEOW!')

c= Cat('kitty')

c.talk()

d=Dog(Animal)

d.talk()

Output - 

Meow!

Woof
answered Mar 31, 2018 by anto.trigg4
• 3,440 points

selected Oct 12, 2018 by Omkar
0 votes

Check the Wikipedia example: it is very helpful at a high level:

class Animal:
    def __init__(self, name):    # Constructor of the class
        self.name = name
    def talk(self):              # Abstract method, defined by convention only
        raise NotImplementedError("Subclass must implement abstract method")

class Cat(Animal):
    def talk(self):
        return 'Meow!'

class Dog(Animal):
    def talk(self):
        return 'Woof! Woof!'

animals = [Cat('Missy'),
           Cat('Mr. Mistoffelees'),
           Dog('Lassie')]

for animal in animals:
    print animal.name + ': ' + animal.talk()

# prints the following:
#
# Missy: Meow!
# Mr. Mistoffelees: Meow!
# Lassie: Woof! Woof!

Notice the following: all animals "talk", but they talk differently. The "talk" behaviour is thus polymorphic in the sense that it is realized differently depending on the animal. So, the abstract "animal" concept does not actually "talk", but specific animals (like dogs and cats) have a concrete implementation of the action "talk".

Similarly, the "add" operation is defined in many mathematical entities, but in particular cases you "add" according to specific rules: 1+1 = 2, but (1+2i)+(2-9i)=(3-7i).

Polymorphic behaviour allows you to specify common methods in an "abstract" level, and implement them in particular instances.

For your example:

class Person(object):
    def pay_bill():
        raise NotImplementedError

class Millionare(Person):
    def pay_bill():
        print "Here you go! Keep the change!"

class GradStudent(Person):
    def pay_bill():
        print "Can I owe you ten bucks or do the dishes?"

You see, millionares and grad students are both persons. But when it comes to paying a bill, their specific "pay the bill" action is different.

answered Oct 12, 2018 by findingbugs
• 4,780 points
0 votes
class Pesron:
    def __init__(self,name,age):
        self.name=name
        self.age=age

class Emp(Pesron):
    def __init__(self,name,age,eno,esal):
        super().__init__(name,age)
        self.eno=eno
        self.esal=esal


    def display(self):
       print("employee Name is :",self.name)
       print("employee Age is :", self.age)
       print("employee NO is :", self.eno)
       print("employee Salary is :", self.esal)

e=Emp("satish",24,101,35000)
e.display()
e=Emp("sunil",27,102,40000)
e.display()

OUTPUT

employee Name is : satish
employee Age is : 24
employee NO is : 101
employee Salary is : 35000
employee Name is : sunil
employee Age is : 27
employee NO is : 102
employee Salary is : 40000
answered Mar 20, 2019 by anonymous

Related Questions In Python

0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
1,228 views
0 votes
1 answer

What is the use of “assert” keyword in Python?

You can try the following in a ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 58,100 points
1,031 views
0 votes
0 answers

what is the use of args and kwargs in python?

can you an example for each? READ MORE

May 11, 2019 in Python by Waseem
• 4,540 points
691 views
0 votes
0 answers

What is the use of get requests in python?

What are the other http requests in ...READ MORE

May 30, 2019 in Python by Waseem
• 4,540 points
437 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,391 views
0 votes
1 answer
+4 votes
7 answers
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
1,257 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP