EuroSciPy 2024

Decorators - A Deep Dive
2024-08-26 , Room 5

Python offers decorator to implement re-usable code for cross-cutting task.
The support the separation of cross-cutting concerns such as logging, caching,
or checking of permissions.
This can improve code modularity and maintainability.

This tutorial is an in-depth introduction to decorators.
It covers the usage of decorators and how to implement simple and more advanced
decorators.
Use cases demonstrate how to work with decorators.
In addition to showing how functions can use closures to create decorators,
the tutorial introduces callable class instance as alternative.
Class decorators can solve problems that use be to be tasks for metaclasses.
The tutorial provides uses cases for class decorators.

While the focus is on best practices and practical applications, the tutorial
also provides deeper insight into how Python works behind the scene.
After the tutorial participants will feel comfortable with functions that take
functions and return new functions.


Python offers decorator to implement re-usable code for cross-cutting task.
The support the separation of cross-cutting concerns such as logging, caching,
or checking of permissions.
This can improve code modularity and maintainability.

This tutorial is an in-depth introduction to decorators.
It covers the usage of decorators and how to implement simple and more advanced
decorators.
Use cases demonstrate how to work with decorators.
In addition to showing how functions can use closures to create decorators,
the tutorial introduces callable class instance as alternative.
Class decorators can solve problems that use be to be tasks for metaclasses.
The tutorial provides uses cases for class decorators.

While the focus is on best practices and practical applications, the tutorial
also provides deeper insight into how Python works behind the scene.
After the tutorial participants will feel comfortable with functions that take
functions and return new functions

Audience

This tutorial is for intermediate Python programmers who want to dive deeper.
Solid working knowledge of functions and classes basics is required.

Outline

  • Examples of using decorators
  • from the standard library
  • from third-party packages
  • Closures for decorators
  • Write a simple decorator
  • Best Practice
  • Use case: Caching
  • Use case: Logging
  • Parameterizing decorators
  • Chaining decorators
  • Callable instances instead of functions
  • Use case: Argument Checking
  • Use case: Registration
  • Class decorators
  • Wrap-up and questions

Abstract as a tweet:

Code re-use via the @ symbol made easy. Learn how to write good decorators.

Category [Scientific Applications]:

Other

Expected audience expertise: Domain:

none

Expected audience expertise: Python:

some

Python user since 1999. Python trainer since 2004. CEO and trainer at Python Academy. Serial Python conference organizer. Chair of Python Software Verband. PSF community award winner.

This speaker also appears in: