Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decorators @ annotation
#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


Messages In This Thread
Decorators @ annotation - by drcl - Feb-23-2024, 04:25 PM
RE: Decorators @ annotation - by deanhystad - Feb-23-2024, 11:49 PM
RE: Decorators @ annotation - by buran - Feb-24-2024, 06:07 AM
RE: Decorators @ annotation - by Gribouillis - Feb-24-2024, 06:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Does @ at sign used for tother than decorators? ggpython000 1 625 Oct-12-2023, 09:08 AM
Last Post: buran
Question How to annotation type? gamecss 4 1,124 Jul-27-2023, 03:03 AM
Last Post: gamecss
  Variable Scopes Decorators oclmedyb 6 2,871 Jan-15-2021, 02:13 PM
Last Post: oclmedyb
  How to extract and label audio using timestamp annotation Mergorine 0 3,574 Nov-30-2020, 04:44 PM
Last Post: Mergorine
  Decorators and return statements JonEdward 2 2,046 Jul-24-2020, 05:02 PM
Last Post: JonEdward
  How to print cache from Decorators with Memoization OlgaM 2 2,167 Jan-29-2020, 05:06 PM
Last Post: OlgaM
  Function Annotation got NameError: name 'xxx' is not defined Lance 6 5,487 Oct-23-2019, 03:13 AM
Last Post: Lance
  Python decorators. dodiko1d 0 11,237 Oct-13-2019, 10:23 AM
Last Post: dodiko1d
  Decorators yksingh1097 2 2,635 Aug-14-2018, 01:44 PM
Last Post: yksingh1097
  learning decorators Prince_Bhatia 6 3,477 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