b = [0]
def copyalist(b):
b = [1, 2, 3]
print(b)
copyalist(b)
print(b)
The outputs are below:
[1, 2, 3]
[0]
The first line indicates that in the function, b was set to [1, 2, 3]; However, when you print(b) out of the function,the second output says that b is still [0].
I don't understand that, why the outer b is not changed?
I also tried b = copy.deepcopy([1, 2, 3]), the outputs are the same.
However, the following code works well:
b = [0]
def copyalist(b):
b += [1, 2, 3]
print(b)
copyalist(b)
print(b)
The outputs are below:
[0, 1, 2, 3]
[0, 1, 2, 3]