2

In this example (which does not work)

def foo(x,y):
   x = 42
   y = y * 2

x = 0
y = 2
foo(x,y)

I would like x = 42 and y = 4.

The idea behind is to have a wrapper to C functions using ctypes:

def foo(self, x, y):
    error = self.dll.foo(self.handler, x, pointer(y))
    if error: 
       self.exception(error)

How can I pass parameters as references in Python?

nowox
  • 25,978
  • 39
  • 143
  • 293

1 Answers1

3

Like @musically_ut, you cannot pass primitive values by reference, but you can get newest values by returning from function. Like this:

def foo(x,y):
   x = 42
   y = y * 2
   return x,y

x = 0
y = 2
x,y=foo(x,y)
Hooting
  • 1,681
  • 11
  • 20