带有 arg returns 与传递的函数相同的装饰器
Decorator with arg that returns the same function as passed
我想要一个将 is 参数作为属性添加到基础函数的装饰器,然后 returns 该函数本身。当我查看模块时,函数 foo 已被删除。它甚至没有出现。
def addarg(x):
def decorator(func):
func.x = x
return func
@addarg(17)
def foo():
pass
print(foo.x) # should print 17
根据@Karl 的评论,您的代码需要添加一行 -
def addarg(x):
def decorator(func):
func.x = x
return func
return decorator # <-- Add this line
@addarg(17)
def foo():
pass
print(foo.x)
这就是所有的人!
我想要一个将 is 参数作为属性添加到基础函数的装饰器,然后 returns 该函数本身。当我查看模块时,函数 foo 已被删除。它甚至没有出现。
def addarg(x):
def decorator(func):
func.x = x
return func
@addarg(17)
def foo():
pass
print(foo.x) # should print 17
根据@Karl 的评论,您的代码需要添加一行 -
def addarg(x):
def decorator(func):
func.x = x
return func
return decorator # <-- Add this line
@addarg(17)
def foo():
pass
print(foo.x)
这就是所有的人!