Polimorphism means that the same function name can be used for different types. This makes programming more intuitive and easier.
def times(x, y): """Return the product of arguments.""" # The arguments should support the * operator (the __mul__ method). return x * y times(2, 3) # 6, int * int times(2, 3.14) # 6.28, int * float times("Bum!", 3) # "Bum!Bum!Bum!", str * int
# Polymorphic len() function. # Based on the __len__ method. len("Python") # 6, the length of a string len(["Python", "C++", "Java"]) # 3, the number of items len({"Name": "Adam", "Age": 30}) # 2, the number of keys len(set([1, 3, 5, 7])) # 4, the number of elements
Polymorphism is a very important concept in Object-Oriented Programming. Polymorphism in class methods: different (unrelated) classes can have a method with the same name.