On the Use and Misuse of Decorators — Christopher Neugebauer

Опубликовано: 02 Сентябрь 2024
на канале: Kiwi PyCon
106
2

Recorded at Kiwi PyCon 2024 - https://kiwipycon.nz/

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 is an Australian developer, speaker, and serial community conference organiser, who presently lives in the United States.

He serves as a Director of the Python Software Foundation, and is co-organiser of the acclaimed North Bay Python conference, a boutique one-track conference run in unusual venues — include an old vaudeville theatre, and more recently a barn on a farm — in Petaluma, California.

Christopher is also a contributor 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.

This video is licensed under CC BY-SA 4.0 - https://creativecommons.org/licenses/...

Recorded and produced by KIT LTD :   / kitltdnz  


Смотрите видео On the Use and Misuse of Decorators — Christopher Neugebauer онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Kiwi PyCon 02 Сентябрь 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 10 раз и оно понравилось людям.