Python From Beginner to Advanced

0% completed

Previous
Next
Python - Polymorphism

Polymorphism is a core concept in object-oriented programming that refers to the ability of different objects to respond to the same method call in different ways. It enhances flexibility and maintainability by allowing one interface to be used for a general class of actions.

Image

Polymorphism in programming comes in two main types: compile-time and run-time polymorphism. Python primarily uses run-time polymorphism, also known as dynamic binding. However, it also supports a form of compile-time polymorphism through method overloading, albeit in a different style compared to statically typed languages.

Compile-time Polymorphism

Compile-time polymorphism, often achieved through method and operator overloading, allows multiple methods or operators to share the same name but behave differently based on their input parameters.

Method Overloading

Method overloading refers to the ability to define multiple methods with the same name but different parameters. Python does not support traditional method overloading as seen in languages like Java or C++. However, you can achieve a similar effect using default parameters or variable-length argument lists.

Example

In this example, we will demonstrate a form of method overloading using default arguments.

Python3
Python3

. . . .

Explanation:

  • def show(self, message="Hello"): Defines a method with a default parameter. If no argument is provided, "Hello" is used as the default.
  • display.show(): Calls the method without arguments, thus using the default.
  • display.show("Goodbye"): Calls the method with the argument "Goodbye", demonstrating how the method adapts based on the input.

Operator Overloading

Operator overloading allows operators to have different meanings based on their context. In Python, you can redefine the behavior of operators for user-defined types by redefining the method that corresponds to the particular operator.

Example

In this example, we will overload the addition operator (+) for a custom class.

Python3
Python3

. . . .

Explanation:

  • def __add__(self, other): Redefines the + operator to add two Point objects.
  • return Point(self.x + other.x, self.y + other.y): Creates a new Point object that is the sum of the coordinates of the two points.
  • This allows the + operator to be used directly with Point objects, showing polymorphic behavior through operator overloading.

Run-time Polymorphism

Run-time polymorphism or dynamic polymorphism is implemented through method overriding and dynamic binding, which allows derived classes to implement and respond differently to methods defined in the base class.

Method Overriding

Method overriding occurs when a derived class provides a specific implementation of a method already defined in its base class.

Example

In this example, we override the speak method in a derived class.

Python3
Python3

. . . .

Explanation:

  • class Dog(Animal): Defines a class Dog that inherits from Animal.
  • def speak(self): In Dog, this method is overridden to provide a dog-specific behavior.
  • This demonstrates how Dog instances use the overridden version of speak, while Animal instances use the base version.

Dynamic Binding

Dynamic binding, also known as late binding, is a powerful concept in object-oriented programming where the method being called upon an object is resolved at runtime rather than compile-time.

This feature is central to polymorphism in Python, allowing for flexible and maintainable code. It enables a program to decide which method to execute only when it is running, depending on the object type or class that invokes the method.

How Dynamic Binding Works

In Python, because of its interpreted nature and lack of type declarations, methods are not bound to the objects at compile time. Instead, the binding occurs when the method is called, which means that the code could behave differently with different inputs, leading to greater flexibility in how objects can be manipulated.

Example

In this example, we will demonstrate dynamic binding through a scenario involving a base class and a derived class with the same method name.

Python3
Python3

. . . .

Explanation:

  • class Bird: Defines a base class Bird with a method fly() that returns a general statement about birds.
    • def fly(self): Method in the Bird class that returns a string indicating that some birds can fly.
  • class Penguin(Bird): Defines a derived class Penguin that inherits from Bird but overrides the fly() method.
    • def fly(self): Overridden method in the Penguin class indicating that penguins do not fly.
  • def bird_flight(bird): A function that accepts an object of type Bird. It calls the fly() method of the passed object, demonstrating polymorphism.
    • print(bird.fly()): This line showcases dynamic binding where the method fly() to be executed is determined at runtime. Depending on the object's class that is passed to bird_flight, a different version of fly() is called.
  • The bird_flight function is called first with a Bird instance and then with a Penguin instance, showing how Python dynamically binds the fly() method to the object type at runtime.

This lesson on polymorphism in Python covers how different forms of polymorphism enhance flexibility and power in object-oriented programming. By understanding and implementing these concepts, you can create more adaptable and maintainable Python applications.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next