Understanding python variable assignment

I am trying to teach myself python (and generally programming) 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 . So, if I wanted to create a new list, b , with the same values ​​as a currently, how would I achieve this?

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 

In this example, I see that x is a new object, but b points to a . Can someone explain to me what is happening here, why x is a new object, but b not?

+6
source share
4 answers

Consider the following 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 practiced using a[1] , we did this by changing it, instead of saying a[1] to refer to a completely new thing.

In your case, when you told x to refer to what refers to a[1] , he took a link to a specific thing, regardless of what was in a[1] at that time, in your case a certain integer number.

Later, when you told a[1] to change, it changed. But what it was referring to did not cease to exist (because x still referred to it there).

By saying x = a[1] , you are not saying that x always refers to what a[1] refers to.

You say that x will refer to the fact that a[1] refers to this moment of assignment.

The same is true for b , just so that the β€œspecific” thing that b talked about to refer to is a whole list that can change the content.

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] 
+1
source

The answer to the second question is that when you x = a[1] x points to an object that is in a[1] , not a[1] .

When changing a[1] you change the object pointed to by a[1] , not the object itself. However, x still points to the old object.

I hope that I have clearly explained this. If you do not comment.

EDIT: Exactly what @jonrsharpe said.

+1
source

The difference here is that a is list , which is mutable (that is, it can be changed in place), but a[1] is int , which is immutable (that is, cannot be changed in place). a[1] = 4 replaces 2 with 4 in list , but x still points to 2 . - @jonrsharpe

 b = a[:] 

Creates a clone a , which is another object. We do this because list mutable, so when we technically occupy a section of another list, for example, here we are, it can refer to a new object.

+1
source

I am also new to python. Based on what I understood so far, everything in python is an object, and variables are just references to these objects.

So, b = a means b points to the same object as.

However, there are two types of objects, mutable and immutable. A list is a mutable object, that is, the actual list referenced by a and b in your code can be modified. Therefore, you see that when you make changes to a, you are actually changing the main list, changing also b.

To create a completely new list b, you can use b = a [:]

0
source

All Articles