views:

176

answers:

2

Can anyone please give me a real life, practical example of Polymorphism? My professor tells me the same old story I have heard always about the + operator. a+b = c and 2+2 = 4, so this is polymorphism. I really can't associate myself with such a definition, since I have read and re-read this in many books.

What I need is a real world example with code, something that I can truly associate with.

For example, here is a small example, just in case you want to extend it.

>>> class Person(object):
    def __init__(self, name):
        self.name = name

>>> class Student(Person):
    def __init__(self, name, age):
        super(Student, self).__init__(name)
        self.age = age
+6  A: 

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.

Arrieta
@Arrieta: Wow, thank you so much! Exactly what I wanted.
Maxx
having an `abstract` method raise `NotImplementedError` breaks `super`.
aaronasterling
+4  A: 

A common real example in Python is file-like objects. Besides actual files, several other types, including StringIO and BytesIO, are file-like. A method that acts as files can also act on them because they support the required methods (e.g. read, write).

Matthew Flaschen