Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : When is a variable passed by reference and when by value? [duplicate]

Possible Duplicate:
Python: How do I pass a variable by reference?

My code :

locs = [ [1], [2] ] for loc in locs:     loc = []  print locs # prints => [ [1], [2] ] 

Why is loc not reference of elements of locs ?

Python : Everything is passed as reference unless explicitly copied [ Is this not True ? ]

Please explain.. how does python decides referencing and copying ?

Update :

How to do ?

def compute(ob):    if isinstance(ob,list): return process_list(ob)    if isinstance(ob,dict): return process_dict(ob)  for loc in locs:    loc = compute(loc)  # What to change here to make loc a reference of actual locs iteration ? 
  • locs must contain the final processed response !
  • I don't want to use enumerate, is it possible without it ?
like image 258
Yugal Jindle Avatar asked Mar 14 '12 05:03

Yugal Jindle


People also ask

Are variables in Python passed by reference or value?

Python always uses pass-by-reference values. There isn't any exception. Any variable assignment means copying the reference value.

What is the difference between pass by value and pass by reference in Python?

Pass by value refers to a mechanism of copying the function parameter value to another variable while the pass by reference refers to a mechanism of passing the actual parameters to the function. Thus, this is the main difference between pass by value and pass by reference.

Is Python by reference or copy?

Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function.

What is the difference between passing by value and passing by reference?

Passing by reference means the called functions' parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself). Pass by value means the called functions' parameter will be a copy of the callers' passed argument.


2 Answers

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 y will 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.

like image 188
Raymond Hettinger Avatar answered Oct 23 '22 23:10

Raymond Hettinger


Everything in Python is passed and assigned by value, in the same way that everything is passed and assigned by value in Java. Every value in Python is a reference (pointer) to an object. Objects cannot be values. Assignment always copies the value (which is a pointer); two such pointers can thus point to the same object. Objects are never copied unless you're doing something explicit to copy them.

For your case, every iteration of the loop assigns an element of the list into the variable loc. You then assign something else to the variable loc. All these values are pointers; you're assigning pointers; but you do not affect any objects in any way.

like image 21
newacct Avatar answered Oct 24 '22 00:10

newacct