What is the difference between slice assignment that slices the whole list and direct assignment?
问题 I see at many places the use of slice assignment for list s. I am able to understand its use when used with (non-default) indices, but I am not able to understand its use like: a_list[:] = [\'foo\', \'bar\'] How is that different from a_list = [\'foo\', \'bar\'] ? 回答1: a_list = ['foo', 'bar'] Creates a new list in memory and points the name a_list at it. It is irrelevant what a_list pointed at before. a_list[:] = ['foo', 'bar'] Calls the __setitem__ method of the a_list object with a slice as