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. Built-in Decorators and Standard Library Decorators in Python
  • 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

  • Built-in Decorators and Standard Library Decorators in Python
    • Built-in Decorators
      • @property
      • @classmethod
      • @staticmethod
      • @dataclass
    • Decorators from functools
      • @functools.lru_cache
      • @functools.cache
      • @functools.wraps
      • @functools.total_ordering
    • Conclusion

Built-in Decorators and Standard Library Decorators in Python

Cours
Fondamentaux
Learn about built-in decorators and standard library decorators in Python, including @property, @classmethod, @staticmethod, @dataclass, and decorators from the functools module.
Author

Remi Genet

Published

2024-10-21

Built-in Decorators and Standard Library Decorators in Python


Python provides several built-in decorators and others in its standard library, particularly in the functools module. These decorators offer powerful functionality for common programming patterns.

Built-in Decorators

@property

The @property decorator is used to define methods in a class that act like attributes:

@classmethod

@classmethod is used to define methods that operate on the class rather than instances:

@staticmethod

@staticmethod is used for methods that don’t need access to the class or instance:

@dataclass

The @dataclass decorator, introduced in Python 3.7, automatically generates special methods like init(), repr(), and eq() for a class:

This decorator simplifies the creation of classes that are primarily used to store data, reducing boilerplate code significantly.

Decorators from functools

The functools module in Python’s standard library provides several useful decorators:

@functools.lru_cache

This decorator implements memoization, caching the results of a function:

@functools.cache

Introduced in Python 3.9, @cache is a simpler unbounded cache:

@functools.wraps

This decorator is used when writing custom decorators to preserve the metadata of the original function:

@functools.total_ordering

This decorator generates missing comparison methods for a class:

Conclusion

These built-in and standard library decorators provide powerful tools for Python developers. They offer efficient solutions for common programming patterns, from property management and method types to caching and comparison operations. Understanding and using these decorators can significantly enhance code readability and performance.

Back to top
Decorator Practically
Practical Decorators in Python Libraries

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