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. Python call Method: A Fun Exploration
  • 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

On this page

  • Python __call__ Method: A Fun Exploration
    • The Infinitely Callable Dog
      • Step 1: Basic Implementation
      • Step 2: Memory Allocation Insights
      • Step 3: Always Return Self
      • Step 4: New Instance Every Call
      • Step 5: Unique Instance Every Time
    • Conclusion

Python call Method: A Fun Exploration

Course
Python Quirks
Explore the quirky side of Python’s call method with fun examples and insights into memory allocation. A lighthearted break in our Python deep dive.
Author

Remi Genet

Published

2024-10-21

Python __call__ Method: A Fun Exploration

After diving into the world of dunder methods, let’s take a fun break and explore some quirky behavior of the __call__ method in Python. This example showcases how we can create objects that can be “called” multiple times, and gives us some interesting insights into Python’s memory allocation.

The Infinitely Callable Dog

Let’s start with a simple goal: we want to create a Dog class that allows us to add any number of parentheses after it. Something like this:

Step 1: Basic Implementation

Let’s start with a simple implementation:

In this implementation, we’re alternating between returning a Dog instance and the Dog class itself. This allows us to chain as many calls as we want!

Step 2: Memory Allocation Insights

Now, let’s look at how Python handles memory allocation in this case:

Interesting! When we create new instances and save them to a variable, Python allocates new memory. But when we chain calls without saving the intermediate results, Python often reuses the same memory address for new instances.

Step 3: Always Return Self

What if we always want to return an instance, regardless of how many times we call it?

Now we always get the same instance, no matter how many times we call it!

Step 4: New Instance Every Call

What if we want a new instance every time?

Interestingly, the address changes after the first call, then remains the same. This is due to Python’s memory management optimizations.

Step 5: Unique Instance Every Time

Finally, if we really want to ensure a unique instance every time:

Now we’re forcing Python to create and track unique instances, resulting in different memory addresses for each new instance.

Conclusion

This fun exploration of the __call__ method and Python’s memory allocation shows us a few things:

  1. The __call__ method allows us to make our objects callable, leading to some interesting and flexible designs.
  2. Python’s memory management is quite clever, often reusing memory addresses when it can.
  3. We can control instance creation and memory allocation by customizing __new__ and __call__.

Remember, while these examples are fun to explore and can teach us a lot about Python’s inner workings, they’re not typically something you’d use in production code. They’re great for understanding Python better and maybe for solving some tricky coding puzzles!

Back to top
Méthodes Dunder en Python
TP: Heritage avec le pricing d’option

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