OOPs

why polymorphism is so powerful in python

KC Mahendra

KC Mahendra

29 June 2023 - -1 min read

Why polymorphism is so powerful in Python:

The word “poly” means many and “morphs” means forms, so it means many forms.

Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations.

Polymorphism gives you the ability to represent objects of different types using a single interface. A real-life example is “You”.

You act like a student when you are at college, you act like a son/daughter when you're at home, and you act like a friend when you're surrounded by your friends.

Advantages of Polymorphism

  1. It increases code readability because related tasks can be grouped together under one name (e.g., “add”).
  2. It allows developers to easily extend existing classes with new features without having to rewrite existing code.
  3. It helps protect data from malicious modifications by allowing only certain methods limited access to sensitive information.
  4. By writing code using polymorphism, you can reduce the redundancy and vulnerability of your applications as well as increase their maintainability and scalability of them over time.
  5. It helps the programmer to reuse the codes, i.e., classes once written, tested, and implemented can be reused as required. Saves a lot of time.
  6. Single variable can be used to store multiple data types. Easy to debug the codes.
  7. Reduces coupling between different functionalities.

Common Uses in Python

Polymorphism is an essential part of object-oriented programming (OOP) concepts in Python, as it allows for redefining functions for different objects without losing their original meaning or purpose.

Types of Polymorphism in Python

polymorphism.png

  • Duck Typing :- This type of polymorphism involves checking only that an object has a certain set of methods or properties, rather than checking its type. If a function expects an object with certain attributes, such as a particular method or property, the code will execute without raising an error even if the object is not the correct type. This makes duck typing very useful in maintaining flexibility while writing code in Python.

  • Method Overloading :- In method overloading, you can define two or more functions with the same name but different arguments. By providing different arguments for each function call, Python will determine which version of the function to call based on how you pass those arguments in each call. This can be used to provide alternative implementations for similar tasks using the same function name but with different numbers of arguments.

  • Operator Overloading :-

    • It allows for reusability; instead of developing numerous methods with minor differences, we can simply write one method and overload it.
    • It also increases code clarity and reduces complexity.
    • Operator overloading also makes the code more concise and easier to understand. Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example, the operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because the '+' operator is overloaded by the int class and str class.
  • Method Overriding :- This type of polymorphism occurs when you define two functions with the same name and arguments in a child class that overrides an existing parent class method. This enables you to extend an existing implementation without modifying it directly. To achieve this concept in Python, use inheritance when creating classes so that child classes can override existing methods from their parent classes.

about the author

Full stack developer having 4 years of industrial experience in designing, development and maintenance of web apps based on Django-Python platforms.worked with different types of relational database(RDBMS) like: Sqlite,mysql,oracle