【发布时间】:2015-02-21 00:37:50
【问题描述】:
我正在尝试编写一个装饰器,它调用两个额外的函数,并在它以特定顺序装饰的函数之外运行它们。
我已经尝试过这些方面的一些东西:
class common():
def decorator(setup, teardown, test):
def wrapper(self):
setup
test
teardown
return wrapper
class run():
def setup(self):
print("in setup")
def teardown(self):
print("in teardown")
@common.decorator(setup, teardown)
def test(self):
print("in test")
最终目标是让装饰器使用以下流程设置 > 测试 > 拆解运行测试。我知道我没有正确调用设置和拆卸。如果我应该如何做到这一点,我将不胜感激,我是使用 python 的新手,我对涉及参数的装饰器的了解是有限的。
【问题讨论】:
-
装饰器成为类的方法有什么意义?您似乎没有使用课程的上下文。
-
对于带参数的装饰器,请参见例如stackoverflow.com/q/5929107/3001761
标签: python function arguments decorator