Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decorators @ annotation
#1
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!
Reply
#2
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.
Reply
#3
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Does @ at sign used for tother than decorators? ggpython000 1 552 Oct-12-2023, 09:08 AM
Last Post: buran
Question How to annotation type? gamecss 4 1,001 Jul-27-2023, 03:03 AM
Last Post: gamecss
  Variable Scopes Decorators oclmedyb 6 2,753 Jan-15-2021, 02:13 PM
Last Post: oclmedyb
  How to extract and label audio using timestamp annotation Mergorine 0 3,444 Nov-30-2020, 04:44 PM
Last Post: Mergorine
  Decorators and return statements JonEdward 2 1,915 Jul-24-2020, 05:02 PM
Last Post: JonEdward
  How to print cache from Decorators with Memoization OlgaM 2 2,081 Jan-29-2020, 05:06 PM
Last Post: OlgaM
  Function Annotation got NameError: name 'xxx' is not defined Lance 6 5,311 Oct-23-2019, 03:13 AM
Last Post: Lance
  Python decorators. dodiko1d 0 8,513 Oct-13-2019, 10:23 AM
Last Post: dodiko1d
  Decorators yksingh1097 2 2,528 Aug-14-2018, 01:44 PM
Last Post: yksingh1097
  learning decorators Prince_Bhatia 6 3,319 Aug-13-2018, 02:28 PM
Last Post: Prince_Bhatia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020