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
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 196 May-13-2024, 05:57 AM
Last Post: Larz60+
  Numeric Enigma Machine idev 9 713 Mar-29-2024, 06:15 PM
Last Post: idev
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 405 Feb-17-2024, 12:29 PM
Last Post: deanhystad
Question Numeric Anagrams - Count Occurances monty024 2 1,545 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,039 Nov-06-2021, 03:26 PM
Last Post: snippsat
  detecting a generstor passed to a funtion Skaperen 9 3,740 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Extract continuous numeric characters from a string in Python Robotguy 2 2,690 Jan-16-2021, 12:44 AM
Last Post: snippsat
  how to modify a variable that is passed as parameter StefanL38 2 2,154 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,598 Sep-07-2020, 08:02 AM
Last Post: perfringo
  How to calculate column mean and row skip non numeric and na Mekala 5 5,042 May-06-2020, 10:52 AM
Last Post: anbu23

Forum Jump:

User Panel Messages

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