Polymorphism

Polymorphism:
Type-based dispatch is useful when it is necessary,but it is not always necessary.Often you can avoid it by writing functions that work correctly for arguments with different types.

Many of the functions we wrote for strings will actually work for any kind of sequence.
examples:

def histogram(s):
d=dict()
for c in s:
if c not in d:
d[c]=1
else:
d[c]=d[c]+1
return d

Function that can work with several types are called polymorphic. Polymorphism can facilate code reuse.

for example of polymorhism:

>>>t1=Time(7,43)
>>>t2=Time(7,41)
>>>t3=Time(7,37)
>>>total=sum([t1,t2,t3])
>>>print total
23:01:00
1.Examples for polymorhism:
len() being used for a string 
print(len("geeks")) 
# len() being used for a list 
print(len([10, 20, 30])) 
Output: 
5
3
Translate »