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

Popular posts from this blog

javascript - Unusual behaviour when drawing lots of images onto a large canvas -

how can i manage url using .htaccess in php? -

javascript - Chart.js - setting tooltip z-index -