Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable assignment and modification (in python) [duplicate]

When I ran this script (Python v2.6):

a = [1,2] b = a a.append(3) print a >>>> [1,2,3] print b >>>> [1,2,3] 

I expected print b to output [1,2]. Why did b get changed when all I did was change a? Is b permanently tied to a? If so, can I make them independent? How?

like image 897
Double AA Avatar asked Jul 22 '11 17:07

Double AA


People also ask

What is variable assignment in Python?

A variable is a string of characters and numbers associated with a piece of information. The assignment operator, denoted by the “=” symbol, is the operator that is used to assign values to variables in Python. The line x=1 takes the known value, 1, and assigns that value to the variable with name “x”.

Does Python allow multiple assignment of variables?

Multiple assignment in Python: Assign multiple values or the same value to multiple variables. In Python, use the = operator to assign values to variables. You can assign values to multiple variables on one line.

How multiple assignment concept works in Python give an example?

The assignment satement also copes nicely with assigning multiple variables at one time. The left and right side must have the same number of elements. For example, the following script has several examples of multiple assignment. We set variables x1 , y1 , x2 and y2 .


2 Answers

Memory management in Python involves a private heap memory location containing all Python objects and data structures.

Python's runtime only deals in references to objects (which all live in the heap): what goes on Python's stack are always references to values that live elsewhere.

>>> a = [1, 2] 

python variables

>>> b = a 

python variables

>>> a.append(3) 

python variables

Here we can clearly see that the variable b is bound to the same object as a.

You can use the is operator to tests if two objects are physically the same, that means if they have the same address in memory. This can also be tested also using the id() function.

>>> a is b >>> True >>> id(a) == id(b) >>> True 

So, in this case, you must explicitly ask for a copy. Once you've done that, there will be no more connection between the two distinct list objects.

>>> b = list(a) >>> a is b >>> False 

python variables

like image 131
Paolo Moretti Avatar answered Sep 28 '22 09:09

Paolo Moretti


Objects in Python are stored by reference—you aren't assigning the value of a to b, but a pointer to the object that a is pointing to.

To emulate assignation by value, you can make a copy like so:

import copy  b = copy.copy(a)  # now the code works as "expected" 

Be aware this has performance disadvantages.

In the case of an array, there's a special method that relies on slices:

b = a[:]  # code also works as expected here 

Update– In addition to this, with some objects you can use the constructor—this includes lists:

b = list(a) 
like image 27
Aaron Yodaiken Avatar answered Sep 28 '22 07:09

Aaron Yodaiken