Other types of decorator

Now that we know what the @ syntax for decorators actually means, we can conclude that it isn't just functions, methods, or classes that can be decorated; actually, anything that can be defined, such as generators, coroutines, and even objects that have already been decorated, can be decorated, meaning that decorators can be stacked.

The previous example showed how decorators can be chained. We first defined the class, and then applied @dataclass to it, which converted it into a data class, acting as a container for those attributes. After that, the @Serialization will apply the logic to that class, resulting in a new class with the new serialize() method added to it.

Another good use of decorators is for generators ...

Get Clean Code in Python 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.