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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -