On the Use and Misuse of Decorators

Sat September 11, 10:30 AM–11:00 AM • Back to program
Session Type Pre-Recorded
Start time 10:30
End time 11:00
Countdown link Open timer

What is a decorator? You might say it's a function that accepts a function and returns a function. That's wrong.

So what's the right answer?

Join us for a deep (but standard talk-length) dive into decorators! You’ll learn what decorators actually are, how they work, how to write your own decorators, and how decorators enable you to do extremely weird things in Python. Most importantly, you’ll understand how to use decorators responsibly, and definitely not to confuse your friends and colleagues.

One of the most fraught questions in Python is “What is a decorator?”

To a new Python developer, it’s a thing with an @ symbol that changes the behaviour of a function or method. To people who want to understand how they work, decorators are functions that change the behaviours of functions by returning new functions.

These definitions are simple, easy to understand, and wrong.

Decorators like @wraps or @classmethod change the behaviour of a callable, and do so by returning a new callable. Other decorators, like @property accept a callable, but leave something completely different in their place.

This flexibility isn’t just there for decorators that come with Python. You can use this flexibility in your own decorators, and it offers the potential for you to build interesting and unexpected new behaviour.

In this talk, we’ll look at why Python has decorators, and how you can build them.

We’ll look at how some more popular decorators in Python do what they do. Finally, we’ll look at how to harness decorators to do unexpected and potentially horrible things with Python.

After this talk, you’ll know how decorators work, how to write your own decorators, and how decorators enable you to do extremely weird things in Python. Most importantly, you’ll understand how to use decorators responsibly, and definitely not to confuse your friends and colleagues.

Christopher Neugebauer he/him/his

Christopher Neugebauer is an Australian developer, speaker, and serial community conference organiser, who presently lives in the United States. He recently served as a Director and Vice-Chair of the Python Software Foundation, and when All This is not currently preventing it, is co-organiser of the acclaimed North Bay Python conference, a boutique one-track conference run in a live music venue in Petaluma, California.

By day, Christopher is an engineer at Toolchain, working on the open source Pants build system, helping make Python’s testing, correctness, and style tools accessible and fast for developers, no matter how big their codebase.