I am a beginner in Python, I cannot understand assignment operator clearly, for example:
list1 = ["Tom", "Sam", "Jim"]
list2 = list1
the above two statements bind 'list1' and 'list2' to ["Tom", "Sam", "Jim"]
, the question is,
if a operator like below:
list1[1] = "Sam's sister"
, if the assignment statement is considered as binding, too, then list2[1]
is still associated with "Sam", the result is that modifying list1
does not affect the list2
, even though Python presents the opposite output, another question is whether list1[1]
can be regarded as a variable as list1
and list2
in Python.
can anyone have any suggestions?
In your example the identifiers list1
and list2
are references to the same underlying object (just different names for the same thing).
id()
can be used to see if the same underlying object is being referenced.
>>> list1 = ["Tom", "Sam", "Jim"]
>>> list2 = list1
>>> id(list1)
44259368
>>> id(list2)
44259368
To create a copy of the defined list use the [:]
notation, or deepcopy
as Matthew has mentioned. You'll notice that when this is done the location/id 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's the object's memory address.)
You're right, they aren't the same. Assignment to a bare name in Python (name = ...
) is a different operation than assignment to anything else. In particular it is different from item assignment (name[0] = ...
) and attribute assignment (name.attr = ...
). They all use the equal sign, but the latter two are manipulable with hooks (__setitem__
and __setattr__
), can call arbitrary code, and are generally under the control of the programmer. Assignment to a bare name is not under the control of the Python programmer. You can't affect what it does; it always just binds the right hand side to the name on the left hand side.
This can be confusing, because people are used to thinking that the equals sign is what makes an "assignment". But in Python, in order to understand what operation is taking place, you really have to look on the left of the equals sign and see what kind of thing is being assigned to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With