7 Comments
User's avatar
Wyrd Smythe's avatar

Cool, I did not know about @wraps, and it never occurred to me to use a class as a decorator. Sweet!

Expand full comment
Stephen Gruppetta's avatar

I now prefer using classes when I need a decorator with arguments. But it’s a very recent shift in preference!

Expand full comment
Wyrd Smythe's avatar

I can see why that would be easier. And I love the idea. I’ve been hooked on OOD since I got into C++ back in the 1990s. (I even wrote an OOP framework for the 8086 assembly work I was doing a lot of back then. 😁😎)

Expand full comment
Bob P's avatar

Just want to say very nicely done Stephen!

Expand full comment
Stephen Gruppetta's avatar

Thanks Bob

Expand full comment
Ranudar's avatar

Thank you for the great refresher about decorators!

This might be the third time I learn them and I have a good passive understanding of them by now but it's always good to bring these concepts back into active memory :-)

Something that tripped me up at least twice when I learned about decorators and especially closures was exactly the example with the nonlocal variable:

Why can't I access a variable although I clearly can access variables from the enclosing scope (e.g. pushing to a list or comparing to a value)?

The answer that I found is: I can(!) access a variable from the outer scope in the sense that I can read it and use methods on it, but I can't *assign* to it directly if I don't tell Python that I want to assign to the variable in the enclosing scope - not create a new local one.

If I want to (re)assign a variable in the enclosing scope using the augmented assignment operatior += I'll need the nonlocal keyword.

I know you know that very well, but maybe some readers also stumbled over this?

Best regards!

Expand full comment
Stephen Gruppetta's avatar

Great way of looking at it! Thanks

Expand full comment