Printing Variable Names Passed to a Function

In some cases, I want to print the debug style output as follows:

# module test.py
def f()
  a = 5
  b = 8
  debug(a, b) # line 18

I want the function to debugprint the following:

debug info at test.py: 18
function f
a = 5
b = 8

I think it should be possible using a validation module to find the stack frame and then find the corresponding line, looking at the source code on that line, getting the argument names from there. The function name can be obtained by moving one stack up. (The values ​​of the arguments are easy to obtain: they are passed directly to the function debug.)

Am I on the right track? Is there a recipe I can reference?

+5
source share
4 answers

You can do something in the following lines:

import inspect

def debug(**kwargs):
  st = inspect.stack()[1]
  print '%s:%d %s()' % (st[1], st[2], st[3])
  for k, v in kwargs.items():
    print '%s = %s' % (k, v)

def f():
  a = 5
  b = 8
  debug(a=a, b=b) # line 12

f()

This produces:

test.py:12 f()
a = 5
b = 8
+3
source

, AOP . , "debug" , , , .

. , post.

+1

, . inspect.getargspec, args, varargs, keywords, defaults, .

import inspect

def f():
  a = 5
  b = 8
  debug(a, b)


def debug(a, b):
    print inspect.getargspec(debug)
f()
+1

. , , getargspec Senthil , -. Btw, getargspec Python 3.0 getfullarcspec .

Python 3.1.2 , :

# from: /questions/184916/python-code-to-get-current-function-into-a-variable/1028426#1028426
def getfunc(func=None, uplevel=0):
    """Return tuple of information about a function

    Go up in the call stack to uplevel+1 and returns information
    about the function found.

    The tuple contains
      name of function, function object, it frame object,
      filename and line number"""
    from inspect import currentframe, getouterframes, getframeinfo
    #for (level, frame) in enumerate(getouterframes(currentframe())):
    #    print(str(level) + ' frame: ' + str(frame))
    caller = getouterframes(currentframe())[1+uplevel]
    # caller is tuple of:
    #  frame object, filename, line number, function
    #  name, a list of lines of context, and index within the context
    func_name = caller[3]
    frame = caller[0]
    from pprint import pprint
    if func:
        func_name = func.__name__
    else:
        func = frame.f_locals.get(func_name, frame.f_globals.get(func_name))
    return (func_name, func, frame, caller[1], caller[2])


def debug_prt_func_args(f=None):
    """Print function name and argument with their values"""
    from inspect import getargvalues, getfullargspec
    (func_name, func, frame, file, line) = getfunc(func=f, uplevel=1)
    argspec = getfullargspec(func)
    #print(argspec)
    argvals = getargvalues(frame)
    print("debug info at " + file + ': ' + str(line))
    print(func_name + ':' + str(argvals))   ## reformat to pretty print arg values here
    return func_name



def df_dbg_prt_func_args(f):
    """Decorator: dpg_prt_func_args - Prints function name and arguments

    """
    def wrapped(*args, **kwargs):
        debug_prt_func_args(f) 
        return f(*args, **kwargs) 
    return wrapped

:

@df_dbg_prt_func_args
def leaf_decor(*args, **kwargs):
    """Leaf level, simple function"""
    print("in leaf")


def leaf_explicit(*args, **kwargs):
    """Leaf level, simple function"""
    debug_prt_func_args()
    print("in leaf")


def complex():
    """A complex function"""
    print("start complex")
    leaf_decor(3,4)
    print("middle complex")
    leaf_explicit(12,45)
    print("end complex")


complex()

:

start complex
debug info at debug.py: 54
leaf_decor:ArgInfo(args=[], varargs='args', keywords='kwargs', locals={'args': (3, 4), 'f': <function leaf_decor at 0x2aaaac048d98>, 'kwargs': {}})
in leaf
middle complex
debug info at debug.py: 67
leaf_explicit:ArgInfo(args=[], varargs='args', keywords='kwargs', locals={'args': (12, 45), 'kwargs': {}})
in leaf
end complex

: wrapped , , , ArgSpec wrapped getfunc debug_prt_func_args. , , .

, : for -loop getfunc, , inspect "", , . , , , , , .

, .

, . ( ) , .

0

All Articles