0

具有如下功能对调试非常有用:

a = np.arange(20)
debug_print(a)

上面应该打印

variable: a
val: [...]

多谢。

4

1 回答 1

1

试试下面的代码:

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(','))
    for name, value in zip(names, args):
        print('{} = {}'.format(name, value))

a = 1
b = 2
debug_print(a, b)
debug_print('a')
debug_print('a,aa') # Not work well for this case.
于 2013-07-11T08:54:14.377 回答