Does a slicing operation give me a deep or shallow copy?

前端 未结 2 766
忘掉有多难
忘掉有多难 2020-11-27 16:53

The official Python docs say that using the slicing operator and assigning in Python makes a shallow copy of the sliced list.

But when I write code for example:

2条回答
  •  半阙折子戏
    2020-11-27 17:48

    You are creating a shallow copy, because nested values are not copied, merely referenced. A deep copy would create copies of the values referenced by the list too.

    Demo:

    >>> lst = [{}]
    >>> lst_copy = lst[:]
    >>> lst_copy[0]['foo'] = 'bar'
    >>> lst_copy.append(42)
    >>> lst
    [{'foo': 'bar'}]
    >>> id(lst) == id(lst_copy)
    False
    >>> id(lst[0]) == id(lst_copy[0])
    True
    

    Here the nested dictionary is not copied; it is merely referenced by both lists. The new element 42 is not shared.

    Remember that everything in Python is an object, and names and list elements are merely references to those objects. A copy of a list creates a new outer list, but the new list merely receives references to the exact same objects.

    A proper deep copy creates new copies of each and every object contained in the list, recursively:

    >>> from copy import deepcopy
    >>> lst_deepcopy = deepcopy(lst)
    >>> id(lst_deepcopy[0]) == id(lst[0])
    False
    

提交回复
热议问题