From the course: Advanced Python: Object-Oriented Programming

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Class decorators

Class decorators

- [Instructor] In this video, we'll discuss class decorators as a class creation utility. You are probably used to decorators of a function such as the built-in lru_cache, which you can use over a function to cache the results, but you can apply decorators to classes as well. You're probably familiar with the data class. We're going to discuss it in other videos. So let's see one usage. We are going to register serializer, a class to serialize for a given media type, say JSON or YAML, maybe message back. So here is our decorator, it's class serializer, and I'm starting with a lowercase s because this is a decorator and people are used to decorators with lowercase name. So it's okay to break the rules from time to time. In the init, we're going to register the media type and here's an example of calling this decorator. This is the init. This is getting called when we're calling it with application/json, for example. Then in the call, this is the application of the decorator on the…

Contents