我可以比@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
如果您无法访问相关模块的源代码,您可以通过检查导入的模块并改变它导出的项目来实现非常相似的效果。
这种方法的极限是无限的。您可以通过将调用存储在某种图形数据结构中,而不是简单地缩进和打印,将其构建为工业级代码分析工具。然后,您可以查询您的数据以回答诸如“此模块中的哪些函数被调用最多?”之类的问题。或“哪些功能最慢?”。事实上,这对图书馆来说是个好主意……