|
|
| Introduction | Variables | If-else |
| Strings | Functions | while-loop |
| For-Loop | List | Set |
| Dictionary | Tuple | Try..Except |
| Class/Object | Inheritance | Polymorphism |
| File Handling | ||
Polymorphism means "One in many forms", which means the ability of an entity to perform differently based on the context. Polymorphism in Python allows the methods to behave differently depending on the object it is working with. It makes code more flexible and reusable. The main advantage of Polymorphism is that it ensures consistent interfaces across different classes and allows code reusability.
The Compile-time polymorphism decides which method or operation to run during compilation through method or operator overloading. Python supports this using default or variable arguments.
"""
This code demonstrates method overloading
using default and variable-length arguments.
The multiply() method works with different
number of inputs, mimicking compile-time
polymorphism.
"""
class PolymorphismDemo1:
def multiplication(self, x=2, y=4, *args):
result = x * y
for varNumber in args:
result *= varNumber
return result
# Create an object
obj = PolymorphismDemo1()
# Using default arguments
print(obj.multiplication())
print(obj.multiplication(4))
# Using multiple arguments
print(obj.multiplication(5, 3))
print(obj.multiply(2, 3, 4))
#Output:
8
4
15
24
"""
This code demonstrates method overloading
with different parameters passed to a
method.
"""
def PrintName(x='Mike', y='John', *arg):
print(f"x: {x}")
print(f"y: {y}")
for z in arg:
print(f"Name arg: {z}")
print(PrintName('Amy','Mary','Kevin'))
print(PrintName('Amy','Mary','Kevin','Josh'))
#Output:
x: Amy
y: Mary
Name arg: Kevin
x: Amy
y: Mary
Name arg: Kevin
Name arg: Josh
In Python, runtime polymorphism occurs through Method Overriding. A child class provides its own version of a method which is already defined in the parent class. Since Python is dynamic, it supports this, allowing same method call to behave differently for different object types. The same method call behaves differently for different object types. Let's see an example of Runtime Polymorphism.
Code Example 3:
#In this example, the infinite while loop breaks when the user input's 'quit':
class Animal:
def make_sound(self):
return "All animals make sound."
class Dog(Animal):
def make_sound(self):
return "Dog: I say bow bow"
class Duck(Animal):
def make_sound(self):
return "Duck: I say quack quack."
class Cat(Animal):
def make_sound(self):
return "Cat: I say meow meow."
#Let's see Polymorphic behavior
animals = [Dog(), Cat(), Duck(),Animal()]
for animal in animals:
print(animal.make_sound())
#Output:
Bark
Meow
All animals make sound.
Explanation: Each object has different response for the make_sound() method call.
Python’s built-in functions like len() and max() are polymorphic they work with different data types and return results based on type of object passed. This showcases it's dynamic nature, where same function name adapts its behavior depending on input.
Code Example 4:
***This code demonstrates polymorphism in Python’s
built-in functions handling strings, lists,
numbers and characters differently
while using same function name.
***
print(len("Hello")) # String length
print(len([1, 2, 3])) # List length
print(max(1, 3, 2)) # Maximum of integers
print(max("a", "z", "m")) # Maximum in strings
#Output:
5
3
3