Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 11 · Decorator patterns at scaleClass-based decorators — when the wrapper needs statepredict152 / 161
+150 XP
Task
📝 **Task:** Predict the 6-line output. A `CallCounter` class wraps `greet` as a decorator. Each call increments `self.count`. After three calls, the count attribute is accessed from OUTSIDE — that's the class-based decorator's superpower over closure-based. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

class CallCounter:
    def __init__(self, fn) -> None:
        self.fn = fn
        self.count = 0
        self.__name__ = fn.__name__

    def __call__(self, *args, **kwargs):
        self.count += 1
        return self.fn(*args, **kwargs)


@CallCounter
def greet(name: str) -> str:
    return f"hi {name}"


print(greet("a"))
print(greet("b"))
print(greet("c"))
print(f"calls: {greet.count}")
print(greet.__name__)
print(type(greet).__name__)

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…