I have a module mymod
which defines a dozen functions with the same signature
def myfun1 (a,b,c):
...
return d
I want to trace all these functions like this:
def traced (f):
def wrapped (a,b,c):
print ">>>%s(%d)" % (f.__name__, (a+b)/c)
d = f(a,b,c)
print "<<<%s(%g)" % (f.__name__, 1/d)
return d
return wrap
@traced
def myfun1 (a,b,c):
...
return d
(here (a+b)/c
and 1/d
are stand-ins for more intricate stuff).
Is there a more elegant way to accomplish this - other than adding @traced
before function definition?
PS. A similar question was asked 5 years ago. I wonder if there are new answers.