Aspect-oriented Programming - Diving deep into Decorators
2023-04-18 , A05-A06

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

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.

Format

The tutorial will be hands on.
I will start with a blank Notebook for each topic and develop the content
step-by-step.
The participants are encouraged to type along.
My typing speed is usually appropriate and allows participants to follow.
The students will receive a comprehensive PDF with all course content as well
Python source code files for all use cases and large code blocks I use.
I will load these files in my Notebook.
The students can do the same or open the files in their preferred editor or IDE.

I also explicitly ask for feedback if I am too fast or things are unclear.
I encourage questions at any time.
In fact, questions and my answers are often an important part of my teaching,
making the learning experience much more lively and typically more useful.

So the participants will be active throughout the whole tutorial.
There will be two exercises that each participant has to do on its own
(or in breakout rooms if the tutorials should be remote) during the tutorial.
We will look at the solutions during the tutorial.
I also supply a solutions PDF after the tutorial.

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

Expected audience expertise: Domain

None

Expected audience expertise: Python

Intermediate

Abstract as a tweet

Effectively programming cross-cutting task with decorators - Code re-use via the @ symbol

I've been a Python user since 1999, teaching Python professionally since 2004.
I am also active in the community organizing Python conferences such as
PyCon DE, EuroSciPy, and BarCamps.
I am a PSF Fellow and chair of the German Python Software Verband.