Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding python variable assignment [duplicate]

Tags:

python

I'm trying to teach myself python (and programming in general) and am a little confused about variable assignment. I understand that if I have

>>> a = [1,2,3]
>>> b = a

that b refers to the same object in memory as a does. So if I wanted to create a new list, b, with the same values as a currently has, how would I achieve that?

Also, consider this example:

>>> a = [1, 2, 3]
>>> b = a
>>> x = a[1]
>>> a[1] = 4
>>> print a, b, x
[1, 4, 3] [1, 4, 3] 2

I see from this example, that x is a new object but b points to a. Could someone explain to me what is going on here, why x is a new object but b isn't?

like image 614
Alex Avatar asked Mar 27 '14 17:03

Alex


People also ask

Does assignment make a copy in Python?

Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.

How does Python variable assignment work?

In Python, variables need not be declared or defined in advance, as is the case in many other programming languages. To create a variable, you just assign it a value and then start using it. Assignment is done with a single equals sign ( = ).

How do you distinguish between copy copy () and copy Deepcopy ()?

copy() create reference to original object. If you change copied object - you change the original object. . deepcopy() creates new object and does real copying of original object to new one. Changing new deepcopied object doesn't affect original object.

Is Python assignment deep copy?

Use assignment = if you want the new changes to affect the original list. Use deep copy if you don't want the new changes to affect the original list. Remember: deep copy makes sure that the newly copied object is not referenced to the original object in any way.


1 Answers

Consider this example:

In [20]: a = [[1], [2], [3]]

In [21]: b = a

In [22]: x = a[1]

In [23]: a
Out[23]: [[1], [2], [3]]

In [24]: b
Out[24]: [[1], [2], [3]]

In [25]: x
Out[25]: [2]

In [26]: a[1][0] = 4

In [27]: a
Out[27]: [[1], [4], [3]]

In [28]: b
Out[28]: [[1], [4], [3]]

In [29]: x
Out[29]: [4]

The difference here is that when we tinkered around with a[1] we did so by modifying it instead of telling a[1] to refer to a whole new thing.

In your case, when you told x to refer to whatever a[1] refers to, it picked up a reference to some concrete thing, whatever was in a[1] at the time, in your case a specific integer.

Later when you told a[1] to change, it did change. But the thing it used to refer to did not stop existing (because x was still there referring to it).

By saying x = a[1] you are not saying x shall always refer to whatever a[1] refers to.

You are saying x shall refer to whatever a[1] refers to at this moment of assignment.

The same is true for b also, it's just that the "concrete" thing that b was told to refer to was a whole list -- which can having changing contents.

Another example:

a = [1, 2, 3,]
b = a

print a, b

# Prints
#[1, 2, 3]
#[1, 2, 3]

a = [4, 5, 6]

print a, b

# Prints
#[4, 5, 6]
#[1, 2, 3]
like image 132
ely Avatar answered Oct 02 '22 17:10

ely