python - How to write a function that can print the name and value of its argument? -
this question has answer here:
it useful debugging have function thus:
a = np.arange(20) debug_print(a)
the above should print
variable: val: [...]
thanks lot.
try following code:
import inspect import re def debug_print(*args): lines = ''.join(inspect.stack()[1][4]) matched = re.search('debug_print\((.*?)\)', lines).group(1) names = map(str.strip, matched.split(',')) name, value in zip(names, args): print('{} = {}'.format(name, value)) = 1 b = 2 debug_print(a, b) debug_print('a') debug_print('a,aa') # not work case.
Comments
Post a Comment