Skip to content
thesarfo

Concept

Polymorphism in Python

Treating different classes as objects of a common superclass, illustrated with a shared display_info function.

views 0

The ability of different classes to be treated as objects of a common superclass.

# Polymorphic function
def display_info(vehicle):
return vehicle.drive()
# Using the polymorphic function
print(display_info(car1)) # Output: Driving Toyota Corolla
print(display_info(electric_car)) # Output: Driving Tesla Model S