From the course: Python: Decorators

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Classes and decorators in Python

Classes and decorators in Python - Python Tutorial

From the course: Python: Decorators

Start my 1-month free trial

Classes and decorators in Python

- [Instructor] We've used decorators with functions. Let's now look at the other place where you work with decorators and that's classes. The best way to understand decorators with classes is to use an example. So let's create a class that counts the number of times, we call the Fibonacci function when determining the number in the sequence. So that's how many times do we call the Fibonacci function to calculate the fourth number in the sequence for example. Let's use the count.pi module as our starting point. So let's start off with our Fibonacci method. With the decorator syntax that we've used all along, the @my decorator syntax is just a cleaner way of decorating the function Pfib using my decorator and then assigning that to Pfib. So this means that if my decorator is a class, it needs to take the function as an argument in its init method. So let's create our class count with the function and our count. So class…

Contents