python中一层装饰器的函数可以带参数吗

2025-05-10 06:05:41
推荐回答(1个)
回答1:

可以的,

装饰器分为可带参数和不可带参数的两种;但是如果带参数,则带参数装饰器的“参数”及“return”必须也是一个装饰器

具体可以参考一下下面的例子:

def deco1(func):
def w(x,*args,**kw):
print "this is deco1"
return x + " decorator"
return w

def deco2(deco):
print "this is deco2"
return deco

@deco2(deco1)
def func(x):
return x

print func("Hello")