echo('foo')疑问):
def echo(**kwargs): for name, value in kwargs.items(): print name, valuefoo = 7echo(foo=foo)
UPDATE: 解决方案
echo(foo)与
inspect
import inspectimport redef echo(arg): frame = inspect.currentframe() try: context = inspect.getframeinfo(frame.f_back).pre_context caller_lines = ''.join([line.strip() for line in context]) m = re.search(r'echos*((.+?))$', caller_lines) if m: caller_lines = m.group(1) print caller_lines, arg finally: del framefoo = 7bar = 3baz = 11echo(foo)echo(foo + bar)echo((foo + bar)*baz/(bar+foo))
输出:
foo 7foo + bar 10(foo + bar)*baz/(bar+foo) 11
它的呼叫次数最少,但是对换行符敏感,例如:
echo((foo + bar)* baz/(bar+foo))
将打印:
baz/(bar+foo)) 11
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)