3 ms·
I think a reasonable programmer who would expect (1) and (2) to behave the same would also expect the following to happen: a = [1, 2]; b = [3, 4] c = a a
by necovek 5y ago
I think a reasonable programmer who would expect (1) and (2) to behave the same would also expect the following to happen:
a = [1, 2]; b = [3, 4]
c = a
a += b
print(c) # [1, 2] — assignment was a copy
print(a) # [1, 2, 3, 4]
So every list assignment would have to be a copy for that mental model not to break easily.
And certainly, there are languages that do that.
Just not Python — for performance reasons, you need to be explicit about copying large chunks of memory, though it includes a fair number of footguns like concatenation of strings and lists, or even slicing — eg. "for a in long_list[:-2]:" will make a duplicate of long_list in memory.