6 ms·
Again, the problem is not what it appears. You're keeping a reference to an existing item rather than making a copy. The results would be just as bad if you pas
by mark-r 13y ago
Again, the problem is not what it appears. You're keeping a reference to an existing item rather than making a copy. The results would be just as bad if you passed in an initial list rather than taking the default.
initial = ['first item']
bag3 = Bag(initial)
bag3.add_item('second item')
print(initial)
I think the surprising thing to most people is that you don't automatically get a copy when you do the assignment. That's how it works in older languages like C and C++, and how it appears to behave when you use immutable objects.