例如,我有以下代码片段:
def func1(num):
print(num)
def func2(num):
func1(num)
def func3(num):
func2(num)
func1(num)
def begin():
pass
def print_graph():
pass
def main():
begin()
func3(3)
print_graph()
有没有什么简单的方法可以打印类似的东西:
func3(1)
func2(1)
func1(1)
func1(1)
我相信,我必须使用 globals()
,但我不知道接下来要做什么。这是某种学习任务,因此我不能使用任何库。
最佳答案
我可以比@jme 做得更好。这是他的装饰器的一个版本,它根据您在调用堆栈中的位置缩进和缩进:
import functools
# a factory for decorators
def create_tracer(tab_width):
indentation_level = 0
def decorator(f): # a decorator is a function which takes a function and returns a function
@functools.wraps(f)
def wrapper(*args): # we wish to extend the function that was passed to the decorator, so we define a wrapper function to return
nonlocal indentation_level # python 3 only, sorry
msg = " " * indentation_level + "{}({})".format(f.__name__, ", ".join([str(a) for a in args]))
print(msg)
indentation_level += tab_width # mutate the closure so the next function that is called gets a deeper indentation level
result = f(*args)
indentation_level -= tab_width
return result
return wrapper
return decorator
tracer = create_tracer(4) # create the decorator itself
@tracer
def f1():
x = f2(5)
return f3(x)
@tracer
def f2(x):
return f3(2)*x
@tracer
def f3(x):
return 4*x
f1()
输出:
f1()
f2(5)
f3(2)
f3(40)
nonlocal
语句允许我们在外部范围内改变 indentation_level
。输入函数后,我们增加缩进级别,以便下一个 print
进一步缩进。然后在退出时我们再次减少它。
这叫做 decorator syntax .它纯粹是“语法糖”;转换为不带 @
的等效代码非常简单。
@d
def f():
pass
与以下内容相同:
def f():
pass
f = d(f)
如您所见,@
只是使用装饰器以某种方式处理装饰函数,并用结果替换原始函数,就像@jme 的答案一样。就像Invasion of the Body Snatchers ;我们正在用看起来类似于 f
但行为不同的东西替换 f
。
如果您坚持使用 Python 2,您可以使用带有实例变量的类来模拟 nonlocal
语句。如果您以前从未使用过装饰器,这对您来说可能更有意义。
# a class which acts like a decorator
class Tracer(object):
def __init__(self, tab_width):
self.tab_width = tab_width
self.indentation_level = 0
# make the class act like a function (which takes a function and returns a function)
def __call__(self, f):
@functools.wraps(f)
def wrapper(*args):
msg = " " * self.indentation_level + "{}({})".format(f.__name__, ", ".join([str(a) for a in args]))
print msg
self.indentation_level += self.tab_width
result = f(*args)
self.indentation_level -= self.tab_width
return result
return wrapper
tracer = Tracer(4)
@tracer
def f1():
# etc, as above
您提到不允许更改现有功能。您可以通过使用 globals()
来改造装饰器(尽管这通常不是一个好主意,除非您真的需要这样做):
for name, val in globals().items(): # use iteritems() in Python 2
if name.contains('f'): # look for the functions we wish to trace
wrapped_func = tracer(val)
globals()[name] = wrapped_func # overwrite the function with our wrapped version
如果您无法访问相关模块的源代码,您可以通过检查导入的模块并改变它导出的项目来实现非常相似的结果。
这种方法没有极限。您可以通过将调用存储在某种图形数据结构中,而不是简单地缩进和打印,将其构建为工业强度的代码分析工具。然后您可以查询您的数据以回答诸如“此模块中的哪些函数被调用最多?”之类的问题。或“哪些功能最慢?”。事实上,这对图书馆来说是个好主意......
关于python - 如何将图形调用打印为树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27557203/