Intermediate Course
Lesson 7 of 100

Class-Based Decorators — Using __call__

Learn to build decorators as classes — enabling stateful decorators that remember how many times they've been called, accumulate data, and reset.

Pyfessor

Lesson Briefing

Pyfessor

"Function-based decorators are stateless by default (nonlocal makes them mutable but awkward). Class-based decorators solve this elegantly: the class instance holds state in regular attributes, and the __call__ method makes the instance callable. This is perfect for things like call counters, rate limiters, circuit breakers, and caches — all stateful decorators that need to remember something between calls. Today you build stateful decorators for the budget dashboard."

Pyfessor

"Hi! I'm Pyfessor. Ready to s-s-s-tart our coding journey?"

Pyfessor
    Pyfessor AI — Learn Python with Your AI Mentor