在此示例中(不起作用)
def foo(x,y):
x = 42
y = y * 2
x = 0
y = 2
foo(x,y)
我希望x = 42
和y = 4
。
背后的想法是使用ctypes包含C函数的包装器:
def foo(self, x, y):
error = self.dll.foo(self.handler, x, pointer(y))
if error:
self.exception(error)
如何在Python中传递参数作为参考?
答案 0 :(得分:3)
与@musically_ut类似,您无法通过引用传递原始值,但您可以通过从函数返回来获取最新值。像这样:
def foo(x,y):
x = 42
y = y * 2
return x,y
x = 0
y = 2
x,y=foo(x,y)