Chapter 6. Function and Method Decorators

A decorator is a function that takes a function or method as its sole argument and returns a new function or method that incorporates the decorated function or method with some additional functionality added. We have already made use of some predefined decorators, for example, @property and @classmethod. In this section we will learn how to create our own function decorators, and later in this short cut we will see how to create class decorators.

For our first decorator example, let us suppose that we have many functions that perform calculations, and that some of these must ...

Get Advanced Python 3 Programming Techniques now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.