Python : When is a variable passed by reference and when by value?
Python : When is a variable passed by reference and when by value?
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Light Drops
--
Chapters
00:00 Question
00:55 Accepted answer (Score 32)
01:45 Answer 2 (Score 67)
03:46 Answer 3 (Score 10)
04:27 Answer 4 (Score 7)
04:53 Thank you
--
Full question
https://stackoverflow.com/questions/9696...
Answer 1 links:
[http://effbot.org/zone/call-by-object.ht...]: http://effbot.org/zone/call-by-object.ht...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #reference #passby-reference #passby-value
#avk47
ANSWER 1
Score 73
Effbot (aka Fredrik Lundh) has described Python's variable passing style as call-by-object: http://effbot.org/zone/call-by-object.htm
Objects are allocated on the heap and pointers to them can be passed around anywhere.
- When you make an assignment such as
x = 1000, a dictionary entry is created that maps the string "x" in the current namespace to a pointer to the integer object containing one thousand. - When you update "x" with
x = 2000, a new integer object is created and the dictionary is updated to point at the new object. The old one thousand object is unchanged (and may or may not be alive depending on whether anything else refers to the object). - When you do a new assignment such as
y = x, a new dictionary entry "y" is created that points to the same object as the entry for "x". - Objects like strings and integers are immutable. This simply means that there are no methods that can change the object after it has been created. For example, once the integer object one-thousand is created, it will never change. Math is done by creating new integer objects.
- Objects like lists are mutable. This means that the contents of the object can be changed by anything pointing to the object. For example,
x = []; y = x; x.append(10); print ywill print[10]. The empty list was created. Both "x" and "y" point to the same list. The append method mutates (updates) the list object (like adding a record to a database) and the result is visible to both "x" and "y" (just as a database update would be visible to every connection to that database).
Hope that clarifies the issue for you.
ANSWER 2
Score 10
It doesn't help in Python to think in terms of references or values. Neither is correct.
In Python, variables are just names. In your for loop, loc is just a name that points to the current element in the list. Doing loc = [] simply rebinds the name loc to a different list, leaving the original version alone.
But since in your example, each element is a list, you could actually mutate that element, and that would be reflected in the original list:
for loc in locs:
loc[0] = loc[0] * 2
ANSWER 3
Score 8
When you say
loc = []
you are rebinding the loc variable to a newly created empty list
Perhaps you want
loc[:] = []
Which assigns a slice (which happens to be the whole list) of loc to the empty list
ANSWER 4
Score 3
Everything is passed by object. Rebinding and mutating are different operations.
locs = [ [1], [2] ]
for loc in locs:
del loc[:]
print locs