如何编写具有多个参数/参数的装饰器?

时间:2019-10-28 20:01:52

标签: python python-3.x decorator python-decorators

我正在尝试创建一个具有多个参数的装饰器。但是,它似乎并不总是在接收解包函数。

def wait(*args, **kwargs):
    print("args == ", args)
    str_kwargs = "\n".join(str(x) for x in kwargs.keys())
    print("kwargs: " + str_kwargs if len(str_kwargs) > 1 else "no kwargs")
    return args[0]

#######################################################
print("begin test 1")
@wait
def print_something_else(something):
    print (something)

#######################################################
print("begin test 2")
def print_something_else(something):
    print(something)
print_something_else = wait(print_something_else, delay=0.2)

#######################################################
print("begin test 3")
@wait(delay=0.2)
def really_print_something_else(something):
    print (something)

对于测试3,args为空。我们仅收到delay。就像我们写了:

really_print_something_else = wait(delay=0.2)

0 个答案:

没有答案