基于Disable global variable lookup in Python(以及我自己的答案),我在使用带有可选参数的函数时遇到了问题,例如在下面这个最小的示例中:
import types
def noglobal(f):
return types.FunctionType(f.__code__, {})
@noglobal
def myFunction(x=0):
pass
myFunction()
基本上,它是这样失败的:
Traceback (most recent call last):
File "SetTagValue.py", line 10, in <module>
myFunction()
TypeError: myFunction() missing 1 required positional argument: 'x'
为什么x
突然被视为必需的参数?你知道吗
这是因为你没有正确地复制函数。如果您查看
types.FunctionType
的签名,您将看到它接受5个参数:您没有传递任何
argdefs
,因此函数不再具有可选参数。复制函数的正确方法是然而,这导致了另一个问题:切断对globals的访问也切断了对内置的访问。如果您尝试使用
print
或open
或dict
或类似的myFunction
,您将得到NameError
。因此,编写decorator的正确方法是:如果要保留默认参数值,还需要传递它们:
您可以将最后一个
closure
参数传递给types.FunctionType
,如果您想让闭包的函数继续工作,您可能还希望从f.__closure__
继承该参数。你知道吗相关问题 更多 >
编程相关推荐