inspect - How to gain information about a function inside a decorator with python? -
using python decorator follows
def decoratorfunctionwitharguments(msg): def wrap(f): def wrapped_f(*args, **kwargs): print ... return f(*args, **kwargs) return wrapped_f return wrap
i want print out following attributes of function f
:
- name of function
- name of module function called.
- line of module function called
i need inspect
module, not quite sure how use method achieve goal. or maybe need evaluate traceback object?
this information can retrieved in cpython sys._getframe:
import sys def mywrapper(msg): def wrap(f): def wrapped_f(*args, **kwargs): f_back = sys._getframe().f_back print f.__name__, f_back.f_code.co_filename, f_back.f_lineno return f(*args, **kwargs) return wrapped_f return wrap @mywrapper("message") def f(x): return x*x f(3)
this output name of function , module , line number called.
Comments
Post a Comment