How to decorate classes in Python

Learn a special use case of Python decorators

Lynn G. Kwong
3 min readNov 15, 2021

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.

Photo by laura adai on Unsplash

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.

--

--

Lynn G. Kwong

I’m a Software Developer (https://youtube.com/@kwonglynn) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.