【发布时间】:2014-11-07 20:20:14
【问题描述】:
是否有可能在不装饰每个函数的情况下捕获类中所有函数的“函数调用之前/之后”事件?可能是一些班级装饰?换句话说,对于这样的代码,我想得到以下输出:
class Foo:
def func1():
print('1')
def func2():
print('2')
c = Foo()
c.func1()
c.func2()
# Output I would like to get:
# func1 called
# 1
# func1 finished
# func2 called
# 2
# func2 finished
我不需要它来追踪。在使用异步函数的类中,我需要知道是否在其他函数完成之前调用了某个函数。
【问题讨论】:
-
输入打印报表?
-
例如。换句话说,我需要在类调用中的任何函数之前/之后调用一些函数。即使我会在课堂上添加新功能。
标签: python python-3.x decorator python-decorators