Python Forum
are numeric types passed by value or reference?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
are numeric types passed by value or reference?
#1
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
Reply
#2
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
Reply
#3
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.
Reply
#4
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.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 8 258 Yesterday, 02:17 PM
Last Post: idev
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 279 Feb-17-2024, 12:29 PM
Last Post: deanhystad
Question Numeric Anagrams - Count Occurances monty024 2 1,475 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 1,962 Nov-06-2021, 03:26 PM
Last Post: snippsat
  detecting a generstor passed to a funtion Skaperen 9 3,468 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Extract continuous numeric characters from a string in Python Robotguy 2 2,584 Jan-16-2021, 12:44 AM
Last Post: snippsat
  how to modify a variable that is passed as parameter StefanL38 2 2,075 Dec-07-2020, 08:39 AM
Last Post: StefanL38
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  How to calculate column mean and row skip non numeric and na Mekala 5 4,835 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,745 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020