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. Decorator Practically
  • 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

On this page

  • Decorators in Python: Concrete Examples
    • Basic Function Decorators
    • Function Decorators with Arguments
    • Simple Class Decorator
    • Decorators with Parameters
    • Conclusion

Decorator Practically

Cours
Fondamentaux
Learn about decorators in Python with concrete examples, including basic function decorators, decorators with arguments, class decorators, decorators with parameters, and their practical applications.
Author

Remi Genet

Published

2024-10-21

Decorators in Python: Concrete Examples


Basic Function Decorators

In Python, decorators are implemented using the @ symbol, followed by the decorator function name, placed above the function definition they’re decorating.

Let’s start with a simple example:

In this example, uppercase_decorator is a function that takes another function as an argument, modifies its behavior (by converting the result to uppercase), and returns the modified function.

Function Decorators with Arguments

Decorators can also work with functions that accept arguments:

This decorator logs the name of the function being called before executing it.

Simple Class Decorator

Decorators can also be applied to classes. Here’s a simple example:

This decorator adds a greet method to the Person class.

Decorators with Parameters

We can create decorators that accept parameters:

This repeat decorator takes an argument specifying how many times the decorated function should be called.

Conclusion

These examples demonstrate the basic usage of decorators in Python. They allow for modifying or enhancing the behavior of functions and classes without changing their source code. Decorators provide a clean and reusable way to extend functionality, making code more modular and easier to maintain.

Back to top
The decorator pattern
Built-in Decorators and Standard Library Decorators in Python

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