Python List Assignment Operator

I am starting in Python, I cannot understand the assignment operator, for example:

list1 = ["Tom", "Sam", "Jim"] list2 = list1 

the above two statements associate "list1" and "list2" with ["Tom", "Sam", "Jim"] , the question is if the statement is as shown below:

list1[1] = "Sam sister" , if the assignment operator is also considered a binding, then list2[1] is still associated with "Sam", the result is that changing list1 does not affect list2 , although Python presents the opposite conclusion, another question is whether list1[1] be considered as a variable like list1 and list2 in python.

Anyone have any suggestions?

+4
source share
4 answers

In your example, the identifiers list1 and list2 are references to the same base object (just different names for the same thing).

id() can be used to see if the same base object is referencing.

 >>> list1 = ["Tom", "Sam", "Jim"] >>> list2 = list1 >>> id(list1) 44259368 >>> id(list2) 44259368 

To create a copy of a specific list, use the notation [:] or deepcopy , as Matthew mentioned. You will notice that when this is done, the location / id value has changed.

 >>> list3 = list1[:] >>> id(list3) 44280208 

About the id command:

 >>> help(id) Help on built-in function id in module __builtin__: id(...) id(object) -> integer Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it the object memory address.) 
+4
source

You are right, they are not the same. Assigning a bare name in Python ( name = ... ) is a different operation than assigning anything else. In particular, it differs from the element assignment ( name[0] = ... ) and attribute assignment ( name.attr = ... ). All of them use an equal sign, but the last two are manipulated with hooks ( __setitem__ and __setattr__ ), can cause arbitrary code and are usually controlled by the programmer. Assigning a bare name is not under the control of a Python programmer. You cannot influence what he does; he always simply ties the right side to the name on the left side.

This can be confusing because people are used to thinking that an equal sign is what an โ€œassignmentโ€ does. But in Python, in order to understand what is happening, you really need to look to the left of the equal sign and see what exactly is assigned.

+3
source

BrenBarn is absolutely right about everything, but here is another way to look at it, which might be easier to understand:

Your first statement creates a list with these values, and then points to list1. The second statement causes list2 to point to the same memory space as in list1. (You can see this by running id on both of them after the second statement).

At this point, list1 and list2 are essentially links to the same modified list. When you change this list, list1 and list2 are still referring to the same actual list, and using to access it will give you the exact same thing.

I made a blog post about a related topic recently and Python Conquers the Universe also talks about a similar topic here .

+1
source

BrenBam has a good explanation of what is happening. deepcopy way around this:

 >>> from copy import deepcopy >>> list1 = ["Tom", "Sam", "Jim"] >>> list2 = deepcopy(list1) >>> list1[1] = "Sam sister" >>> list1 ['Tom', "Sam sister", 'Jim'] >>> list2 ['Tom', 'Sam', 'Jim'] 

A good programming style will make him rarely use deepcopy , though.

0
source

Source: https://habr.com/ru/post/1439721/


All Articles