So I read this page about decorators, but I still don't understand when decorators are useful.
Consider a piece of code defining a function f
, and then calling it multiple times. For some reason we want f
to do some extra work. So we have 2 ways to do it:
- define a new function
g
which callsf
, and does the extra work needed. Then in the main code, replace all calls tof
by calls tog
- define a decorator
g
, and edit the code to add@g
before calls tof
In the end, they both achieve the same result and the advantage of 2) over 1) is not obvious to me. What am i missing?