Programmation Orientée Objet en Python
  • Back to Main Website
  • Home
  • Introduction: Histoire et Concepts
    • Introduction: Histoire et Concepts
    • Histoire de la programmation
    • Première Structuration des données
    • Naissance de la POO
    • Python: tout n’est qu’objet
    • Python : Simplicité des objets et performance sous-jacente
    • Classes en Python : Concepts fondamentaux

    • Travaux Pratiques
    • Construire sa propre Liste
    • Construire son propre DataFrame
  • Encapsulation, Héritage, Composition et Dunder
    • Encapsulation, Heritage, Composition et Dunder
    • Encapsulation en Python
    • Héritage en Python : Concept et intérêt
    • Héritage vs Composition
    • Méthodes Dunder en Python
    • Python call Method: A Fun Exploration

    • Travaux Pratiques
    • TP: Heritage avec le pricing d’option
    • TP : Ajouter des méthodes dunder à DataFrameSimple
    • TP : Étendre la classe Liste avec des méthodes dunder
    • TP: Dunder Method with Tensor for Automatic Differentiation
  • Polymorphisme et Surcharge
    • Polymorphisme et Surcharge
    • Polymorphism in Object-Oriented Programming
    • Polymorphism in Python: Function Overloading and Type Checking
    • Class Creation: Standard vs type()
    • Type Hinting, Typing Module, and Linters in Python
    • Abstract Classes
    • Protocol Classes

    • Travaux Pratiques
    • TP
  • Decorators
    • Design Patterns
    • The decorator pattern
    • Decorator Practically
    • Built-in Decorators and Standard Library Decorators in Python
    • Practical Decorators in Python Libraries

    • Travaux Pratiques
    • TP: Monte Carlo Option Pricing with Decorators
    • TP: Optimizing Heston Model Monte Carlo Simulation
  • Project Management and Packaging
    • Project and Package
    • Organizing Python Projects
    • Understanding imports
    • Python Package Management and Virtual Environments
    • Unit Testing in Python

    • Travaux Pratiques
    • TP: Creating a Linear Regression Package
  • Design Patterns
    • OOP Design Patterns
    • Python-Specific Design Patterns
    • Creation Design Patterns
    • Structural Design Patterns
    • Behavioral Design Pattern

    • Travaux Pratiques
    • TP
  • Sujets de Projets possibles
    • Projets
    • Projets POO - 2024-2025
  • Code source
  1. Polymorphism in Python: Function Overloading and Type Checking
  • Polymorphisme et Surcharge
  • Polymorphism in Object-Oriented Programming
  • Polymorphism in Python: Function Overloading and Type Checking
  • Class Creation: Standard vs type()
  • Type Hinting, Typing Module, and Linters in Python
  • Abstract Classes
  • Protocol Classes
  • Travaux Pratiques
    • TP

On this page

  • Polymorphism in Python: Function Overloading and Type Checking
    • Function Overloading in Python
      • The C++ Approach (Not Available in Python)
      • Python’s Approach
    • Polymorphism Through Type Checking
      • Benefits of This Approach:
      • Example Usage:
    • Duck Typing and Polymorphism in Python
    • Conclusion

Polymorphism in Python: Function Overloading and Type Checking

Cours
Fondamentaux
Learn how to achieve polymorphism in Python through function overloading, type checking, and class-based polymorphism
Author

Remi Genet

Published

2024-10-21

Polymorphism in Python: Function Overloading and Type Checking


In Python, function overloading (having multiple functions with the same name but different parameters) is not directly supported like in C++. However, Python offers alternative ways to achieve similar functionality. Let’s explore this concept and how it relates to polymorphism.

Function Overloading in Python

The C++ Approach (Not Available in Python)

In C++, you might write:

class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }
    
    double add(double a, double b) {
        return a + b;
    }
};

This is not possible in Python. If you define two functions with the same name, the latter will override the former.

Python’s Approach

In Python, we need to use different strategies to handle different types of inputs:

  1. Different Function Names:

  2. Single Function with Type Checking:

  3. Using a Class to Encapsulate Different Implementations:

Polymorphism Through Type Checking

The class-based approach above demonstrates a form of polymorphism. The add method behaves differently based on the types of its inputs, effectively providing different implementations for different data types.

Benefits of This Approach:

  1. Single Interface: Users of the Calculator class only need to call add, regardless of the input types.
  2. Encapsulation: The type-specific implementations (_add_integers and _add_floats) are hidden from the user.
  3. Extensibility: It’s easy to add support for new types by adding new private methods and extending the type checking in add.

Example Usage:

Duck Typing and Polymorphism in Python

While the above approach uses explicit type checking, Python often relies on duck typing for polymorphism. “If it walks like a duck and quacks like a duck, it’s a duck.”

In this case, calculate_area works with any object that has an area method, regardless of its actual type.

Conclusion

While Python doesn’t support function overloading in the same way as C++, it offers flexible alternatives through type checking and duck typing. These approaches allow for polymorphic behavior, enabling functions and methods to work with different types in a way that’s both pythonic and maintainable.

The class-based approach with encapsulated type-specific methods provides a clean way to handle different input types while maintaining a simple interface for the user. This demonstrates how Python can achieve polymorphic behavior similar to function overloading in other languages, but with its own unique style.

Back to top
Polymorphism in Object-Oriented Programming
Class Creation: Standard vs type()

Programmation Orienté Object en Python, Rémi Genet.
Licence
Code source disponible sur Github

 

Site construit avec et Quarto
Inspiration pour la mise en forme du site ici
Code source disponible sur GitHub