How to decorate classes in Python
Learn a special use case of Python decorators
We introduced how to decorate functions in Python in a previous post. If you haven’t checked that post yet, it’s highly recommended to do so because the fundamentals of decorators in Python are introduced in detail in that post. In this post, we will focus on how to decorate classes in Python.
Let’s first create a simple class in which the decorators will be applied:
It’s common to decorate the methods of classes, which works exactly the same as decorating common functions as demonstrated in the previous post. Let’s use the same simple decorator to decorate the dial
method of the Phone
class:
Let’s see how the function decorator works when it’s applied to the dial
method of the Phone
class.