Passing an integer by reference in Python

144,749

Solution 1

It doesn't quite work that way in Python. Python passes references to objects. Inside your function you have an object -- You're free to mutate that object (if possible). However, integers are immutable. One workaround is to pass the integer in a container which can be mutated:

def change(x):
    x[0] = 3

x = [1]
change(x)
print x

This is ugly/clumsy at best, but you're not going to do any better in Python. The reason is because in Python, assignment (=) takes whatever object is the result of the right hand side and binds it to whatever is on the left hand side *(or passes it to the appropriate function).

Understanding this, we can see why there is no way to change the value of an immutable object inside a function -- you can't change any of its attributes because it's immutable, and you can't just assign the "variable" a new value because then you're actually creating a new object (which is distinct from the old one) and giving it the name that the old object had in the local namespace.

Usually the workaround is to simply return the object that you want:

def multiply_by_2(x):
    return 2*x

x = 1
x = multiply_by_2(x)

*In the first example case above, 3 actually gets passed to x.__setitem__.

Solution 2

Most cases where you would need to pass by reference are where you need to return more than one value back to the caller. A "best practice" is to use multiple return values, which is much easier to do in Python than in languages like Java.

Here's a simple example:

def RectToPolar(x, y):
    r = (x ** 2 + y ** 2) ** 0.5
    theta = math.atan2(y, x)
    return r, theta # return 2 things at once

r, theta = RectToPolar(3, 4) # assign 2 things at once

Solution 3

Not exactly passing a value directly, but using it as if it was passed.

x = 7
def my_method():
    nonlocal x
    x += 1
my_method()
print(x) # 8

Caveats:

  • nonlocal was introduced in python 3
  • If the enclosing scope is the global one, use global instead of nonlocal.

Solution 4

Maybe it's not pythonic way, but you can do this

import ctypes

def incr(a):
    a += 1

x = ctypes.c_int(1) # create c-var
incr(ctypes.ctypes.byref(x)) # passing by ref

Solution 5

Really, the best practice is to step back and ask whether you really need to do this. Why do you want to modify the value of a variable that you're passing in to the function?

If you need to do it for a quick hack, the quickest way is to pass a list holding the integer, and stick a [0] around every use of it, as mgilson's answer demonstrates.

If you need to do it for something more significant, write a class that has an int as an attribute, so you can just set it. Of course this forces you to come up with a good name for the class, and for the attribute—if you can't think of anything, go back and read the sentence again a few times, and then use the list.

More generally, if you're trying to port some Java idiom directly to Python, you're doing it wrong. Even when there is something directly corresponding (as with static/@staticmethod), you still don't want to use it in most Python programs just because you'd use it in Java.

Share:
144,749
CodeKingPlusPlus
Author by

CodeKingPlusPlus

Updated on October 31, 2021

Comments

  • CodeKingPlusPlus
    CodeKingPlusPlus over 2 years

    How can I pass an integer by reference in Python?

    I want to modify the value of a variable that I am passing to the function. I have read that everything in Python is pass by value, but there has to be an easy trick. For example, in Java you could pass the reference types of Integer, Long, etc.

    1. How can I pass an integer into a function by reference?
    2. What are the best practices?
    • robert
      robert over 9 years
      see this for a nice if slightly convoluted way to wrap your ints in an anonymous class (which is mutable) which will behave like a 'reference': stackoverflow.com/a/1123054/409638 i.e. ref = type('', (), {'n':1})
  • mgilson
    mgilson about 11 years
    It's no wonder we get confused with the terminology. Here we have it described as call-by-object and here it is described as pass-by-value. Elsewhere it's called "pass-by-reference" with an asterisk on what that actually means... Basically, the problem is that the community hasn't figured out what to call it
  • user102008
    user102008 about 11 years
    Right and also I would rather not mention "passing" at all, because that is what people are confused about. In Python, every expression evaluates to a reference (i.e. all values are references). When you create an object, you get a reference. When you call a function, it returns a reference. When you access an attribute, the thing on the left is a reference. Basically, it's all references. Anything done to objects must be done through references that point to it. So it's no surprise that when you pass things, they are also references.
  • user102008
    user102008 about 11 years
    Python references are the exact same as Java references. And Java references are according to the JLS pointers to objects. And there is basically a complete bijection between Java/Python references and C++ pointers to objects in semantics, and nothing else describes this semantics as well. "They don't have to be dereferenced" Well, the . operator simply dereferences the left side. Operators can be different in different languages.
  • Luis Masuelli
    Luis Masuelli about 10 years
    JAVA doesn't pass integers by reference (integers or any object. boxed objects are replaced as well upon assignment).
  • Astery
    Astery about 8 years
    I think, because everything is passed by value is not really true. Quote docs: arguments are passed using call by value (where the value is always an object reference, not the value of the object)
  • Kroltan
    Kroltan about 7 years
    @LuisMasuelli Well, boxed primitves are treated just like objects, the only thing preventing their use as the OP wants is the fact that boxes are immutable (and since the primitives themselves are also immutable, the whole thing is immutable and can only be changed at variable level)
  • AN88
    AN88 over 6 years
    Lists, objects, and dictionaries are passed by reference
  • B Bulfin
    B Bulfin over 6 years
    If that were true, assignment to a parameter in a function would be reflected at the call site. As Astery quoted passing is by value and those values are object references.
  • markrages
    markrages over 6 years
    Or wrap the number in a class: github.com/markrages/python_mutable_number
  • z33k
    z33k almost 5 years
    A good use case for this is counting a number of calls (a total, not depth) inside a recursive function. You need a number that can be incremented in all branched out calls. A standard int just doesn't cut it
  • user2585501
    user2585501 over 2 years
    Pass scalar by reference support would be useful for cross-language compatibility (eg converting c++ to python)
  • Ron Inbar
    Ron Inbar over 2 years
    How is this different than C#, Java, or even JavaScript, where numbers are passed by value and objects are passed by reference?
  • mgilson
    mgilson over 2 years
    There is no pass-by-value -- calling a function does not copy an object (as it does in a pass-by-value context in C-like languages). IDK how this works under the hood in Java and I don't know any C#. But this does look a lot like JavaScript. Passing an object to the function just makes another handle to that object in the function's stack frame. Some objects are immutable (like numbers) so you can't change them in the function -- other objects can be mutated.
  • Ron Inbar
    Ron Inbar over 2 years
    @mgilson I don't think that numbers, for example, are really passed by reference, except, perhaps, for very large integers that take up more than one machine word. That would be terribly inefficient. In C#, when you need to handle a number (or any other "value type") as an object (for example, to call one of the Int32 type's methods), it is "boxed", i.e. allocated on the heap and replaced with a reference to that copy. The rest of the time its value is copied directly.
  • mgilson
    mgilson over 2 years
    @RonInbar -- I'm not really sure what you're getting at here. Python isn't C# (or Java) with a concept of boxing and unboxing. In the reference implementation, everything is just a PyObject. When you call a python function, python doesn't know (or care) about the type that is being passed. You can call the same function a bunch of times with different types in fact (consider the builtin str). There isn't special casing for primitives at the function interface level AFAIK. And yes, this is one reason that python function calls are a bit expensive by comparison.
  • Smiley1000
    Smiley1000 over 2 years
    Yes, writing a simple wrapper class and simply always accessing the internal object is the way to go.
  • NeronLeVelu
    NeronLeVelu almost 2 years
    but you need to use only 1 specific variable name for this. This reduce the interest of "by reference" if it's the exact know reference