+ 1
Python variable question - how does this work
a = [1,2,3] b = a b.append(5) a.append(4) print(a) print(b) Result [1,2,3,5,4] [1,2,3,5,4] Why isn't a [1,2,3,4] and b [1,2,3,5]? Edit: And why is this different a = "hello" b = a a += "world" b += "!" print(a) print(b) Result Helloworld Hello!
11 Antworten
+ 5
The reason is that b = a creates a shallow copy of a. That means that a and b refer to the same object, not to two identical objects.
For more information about shallow and deep copies:
https://realpython.com/copying-JUMP_LINK__&&__python__&&__JUMP_LINK-objects/
+ 7
because b is referring to a when you give b=a.So if you change the value of b or a it will affect both.
If you want to create duplicate instead of reference, you can use list slicing .Like that:
b=a[:]
https://code.sololearn.com/c4i4vuKlD5J2/?ref=app
+ 4
When you assign things in python they then reference to the same location . So any change to values stored in variable a or b will affect both .
+ 2
Yvonne Rizkallah In Python, everything is an object and every initialized variable holds the reference to an object present in the heap. Here's what happens in your code:
# Create an array object and store its reference in variable 'a'.
a = [1, 2, 3]
# Assign this reference contained in variable 'a' to variable 'b'.
b = a
# Call the append method of the list object
b.append(5)
a.append(4)
# Since both 'a' and 'b' are the same (as mentioned earlier), printing 'a' and 'b' shows the same result.
Now, considering the edit:
# This creates a new instance (a new string object) of the str class and assigns its reference (i.e., the memory location that contains the data related to the object) to variable 'a'.
a = "hello"
# This copies the reference contained in variable 'a' to variable 'b'
b = a
# This creates 2 different str objects, the first one being the 'world' string and the second one being a + 'world', the concatenated version (note: a+="world" is the same as a = a + "world")
a += "world"
Continued...
+ 2
# Do the same thing to variable 'b'
b += "!"
Clearly, both the variables contain different references to two different string objects, hence the differing outputs. I hope that this explanation helps.
+ 2
Yvonne Rizkallah The assignment is the same. However, what happens next is not. In the first case, a method is called. Both the variables 'a' and 'b' refer to the same object. So, the method can be called in either one. But the second case is a bit different. When concatenating strings, a new string object is created and the reference to this is returned. Since strings are immutable, the original string can't be modified. The only way is to make a new string object. 'a + "world"' and 'b + "!"' create two new string objects, even though 'a' and 'b' were initially the same. Please read my previous comment to understand about this better.
+ 2
The reason it didn't work for the list because a and b are pointing to the same object as what Simon Sauter said but, you can instead do:
a = [1, 2, 3]
b = a[:]
print(a)
print(b)
result:
[1, 2, 3, 4]
[1, 2, 3, 5]
Which will create a copy of the list stored in a and store it in b
https://code.sololearn.com/c6FI3vR1PcxV/?ref=app
+ 2
Or you can just use the list.copy() method to make a distinct, but duplicated copy of the original list.
Note: Only the list object is copied, not the objects inside them.
+ 2
Calvin Thomas Right!
+ 1
Calvin Thomas why is the list assignment different than a string assignment
+ 1
The reason is that you created a shallow copy as b=a when you assign anything then they refer the same location