Decorators are powerful tools to hide complexity in a program and improve the readability of the code. Decorators allow to wrap functions and inject additional code to get executed before and/or after a function call. They can also be used to partly substitute metaclasses.
These sophisticated patterns are crucial for writing robust Python code, especially since they are often complex enough to be challenging for current coding agents and large language models to generate correctly, making human mastery essential.
Especially beginners may wonder about the @-syntax and what kind of magic happens there.
In this tutorial I will uncover this magic and we learn about function-wrapping and accessing namespaces by closures. Both the function-based and the class-based approach will be introduced, as well as best practice.
This tutorial is partly hands-on with a workshop style: a laptop with a Python installation is required. There will be time for exercises at different levels to get comfortable with the topics.
Some experience: attendees should be comfortable with Python functions. Also a basic understanding of classes is helpful.
After the tutorial students will walk out with a basic understanding about the underlying mechanism of decorators and will be able to write at least basic decorators on their own.
Dr. Klaus Bremer got in contact with Python about 1999, began programming even earlier and is maintainer of several open-source libraries. In 2014, he started giving professional trainings, which have summed up since then to more than 200 courses (most often multi-day workshops), from basic introductions up to advanced and specialized topics.