小编Mar*_*kus的帖子

带参数的装饰器

代码如下

def my_dec(func):
    def wrap(w):
        t = func(w)
        return t * 4
    return wrap


@my_dec
def testing(n):
    return n


new = testing(3)
print(new)  # This prints 12
Run Code Online (Sandbox Code Playgroud)

这个示例工作正常,但是现在我尝试将以下内容添加到装饰器中@my_dec(100),我需要将给定的数字乘以100。

当我尝试这个

@my_dec(100)
def testing(n):
    return n
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

def my_dec(func):
    def wrap(w):
        t = func(w)
        return t * 4
    return wrap


@my_dec
def testing(n):
    return n


new = testing(3)
print(new)  # This prints 12
Run Code Online (Sandbox Code Playgroud)

如何将其传递100给装饰器?

python decorator python-decorators

3
推荐指数
1
解决办法
474
查看次数

标签 统计

decorator ×1

python ×1

python-decorators ×1