# **Polymorphism:**
Polymorphism refers to the ability of an object to take many forms. The best example to understand this concept is a function using the "in" keyword. We can use it with lists, tuples, strings, etc., and it behaves a bit differently with each, but the same function is able to deal with all these different data types.
Polymorphism in Python is achieved in several ways, but the most common one is through method overriding and interfaces. Here is an example using method overriding:
```python
class Animal:
def sound(self):
return "Generic sound"
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
def animal_sound(animal):
print(animal.sound())
animal_sound(Dog()) # Outputs: "Bark"
animal_sound(Cat()) # Outputs: "Meow"
```
In this example, the animal_sound function is able to take in any object that inherits from the Animal class and call the sound method on it. The correct version of the sound method is determined at runtime, based on the type of the object passed in. This is polymorphism in action.
In this example, the animal_sound function is able to take in any object that inherits from the Animal class and call the sound method on it. The correct version of the sound method is determined at runtime, based on the type of the object passed in. This is polymorphism in action.