python - print arguments on function invocation -
wondering easiest way debug prints.
i looking though code , there functions use lot of generic text arguments inputs. looking easy way print line without having modify avoid getting typos or errors in there
simple example, line in code
somefunction('%s_somestring', variable, '%s_morestrings' %someotherstring, somemorestring, someobject) so instead of checking
print 'somefunction('%s_somestring', %s, '%s_morestrings', 'someobject') % (someotherstring, variable, somemorestring) i looking way print that. tried writing function print string , eval it, not work way intend
somefunction('someotherstring_somestring', variable, 'somemorestring_morestrings', someobject) is there way that
here's simple implementation:
from functools import wraps def prints_invocation(f): @wraps(f) def wrapped(*args): print "%s(%s)" % (f.__name__, ', '.join(repr(a) in args)) return f(*args) return wrapped >>> @prints_invocation ... def add(x, y): ... return x + y >>> add(1, 2) add(1, 2) 3 it trivial extend print kwargs , return values
Comments
Post a Comment