Feb-24-2024, 06:12 AM
(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 »