Python Forum

Full Version: are numeric types passed by value or reference?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I would like to know if python passed certain data types by value, and other data types by reference.
Here is an example:

def multiply(myArg):
    myArg = myArg * 2
    return myArg   
y = 5    
multiply(y)
print y # Result is 5, so the variable Y was passed by value?



def appendToList(myList):
    myList.append("luululu")
    
someList = list()    
appendToList(someList)
print someList # Result: ['luululu'], so it was passed by reference?
As you can see the variable y was not modified, whereas the list was modified. So are numeric types passed by value and lists, tuples and other objects passed by reference?

Thank you
Python passes by values when you pass variables directly like the way you passed “y”. If the variable points to mutable objects like lists and all, it will pass the value by reference
And by the way: You should start switching from Python 2.7 to Python 3.6 at least.
Python 2.7 is deprecated in 6 weeks.
Python always passes references to python objects. But it cannot pass a reference to a name. When you call multiply(y), a reference to a python object representing the number 5 is passed because the current value of y is that python object. But it doesn't pass a reference to the variable y because this is not possible, so y is not modified by the call.
(Nov-18-2019, 07:06 PM)ThomasL Wrote: [ -> ]And by the way: You should start switching from Python 2.7 to Python 3.6 at least. Python 2.7 is deprecated in 6 weeks.
In the vfx industry we are stuck for now with python 2.7 unfurtonately.