3 ms·
Agreed. The deep copy is much much better. Consider this code: >>> a = [1, [2, 3]] >>> b = list(a) # or a[:], they are identical >>> a[1].append(4) >>> a.app
by ggrot 18y ago
Agreed. The deep copy is much much better. Consider this code:
>>> a = [1, [2, 3]]
>>> b = list(a) # or a[:], they are identical
>>> a[1].append(4)
>>> a.append(5)
>>> a
[1, [2, 3, 4], 5]
>>> b
[1, [2, 3, 4]]
If you had used copy.deepcopy, b would now be [1, [2, 3]] as intended.
- diN0bot 18y agothese are good points. of course, what with classes i very rarely have deep lists anyway. in fact, copying lists is seldom an issue i run into (though i always run a few tests at the prompt to make sure my understanding is right because mutability bugs can be hard to track down later).