Python Forum
Decorators @ annotation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Decorators @ annotation (/thread-41656.html)



Decorators @ annotation - drcl - Feb-23-2024

I'm not new to coding but new to Python. Decided to go through the CS50P course to get up to speed. Early on decorators were introduced which I thought was odd. I am familiar with decorators in Java/ general OOP and understand the concept but haven't really used them.

As the course is introducing them early on I assume they will be important to the work on the course. As I said, fine with the general concept but the @annotation is really causing me a problem. I understand it but I can't see why anyone would use it in production.

Maybe I am missing something but it seems to me that it totally couples the original function to the decorator. How is that good if I want to use my function in other decorators? I feel like I would never use the @ as I would be thinking about how inflexible it is. Is my OOP clean code indoctrination making me see something that isn't there?

I have tried to find an answer to this but every tutorial and article uses the same basic examples and doesn't discuss this problem. Is it a problem or not? That makes me feel like I am missing something fundamental - some big difference between my normal OOP understanding of decorators.

To hit such a mental roadblock so early on is frankly demoralising - I thought Python would be very straightforward!

Please help free my mind!


RE: Decorators @ annotation - deanhystad - Feb-23-2024

These are the decorators that I use:

Often
@classmethod
@staticmethod
@property

Less often
@dataclass
@ABC.abstractmethod
@functools.singledispatchmethod
@typing.overload

Other than those, I don't user decorators much, probably because I'm not doing much with an existing code base.


RE: Decorators @ annotation - buran - Feb-24-2024

(Feb-23-2024, 04:25 PM)drcl Wrote: How is that good if I want to use my function in other decorators?
You use the decorator on your function, not your function in decorators.
Actually the decorators are just a syntactic sugar to pass a function into other function

Also maybe have a look at Is a Python Decorator the same as Java annotation, or Java with Aspects?

Other useful SO question to understand decorators How do I make function decorators and chain them together?

Primer on Python Decorators



RE: Decorators @ annotation - Gribouillis - Feb-24-2024

(Feb-23-2024, 04:25 PM)drcl Wrote: Maybe I am missing something but it seems to me that it totally couples the original function to the decorator. How is that good if I want to use my function in other decorators?
You could say the same with inner functions, see this code
def spam(x):
    def square(x):
        return x * x
    y = square(x)
    return y + square(y)
Inner functions are very often used in code. Clearly, if you want to use the square() function in other contexts, you must first extract it from the spam() function and define it at module level.

In the same way, if want to reuse a decorated function, you must first extract it from its decorator and define it independently at module level.