Polymorphism

Polymorphism comes from two Greek words meaning ‘many shapes’ and is best explained by a real world example. The feet of a human are a very different shape to that of a centipede, but they have basically the same function, walking. So we can refer to the feet of a human, centipede, horse, or frog without confusion. Polymorphism allows the programmer to give the same names to methods and attributes that play similar roles in distinctive classes.
As an example we will use a built-in operator, function or method on an object without knowing what kind of object it is.

Here we use the count method on a string.

a = 'This is a string'
print a.count('s')
>>>
3

Now we will use the count method on a list.

b = [1,2,3,4,5,'s','i','n']
print b.count('s')
>>>
1

Tags: