Is the official documentation on Python.org wrong, or did I interpret something wrongly?
Near the end of the Lists Section of the documentation in "An Informal Introduction to Python", one can find the following description about copying lists:
Simple assignment in Python never copies data. When you assign a list to a variable, the variable refers to the existing list. Any changes you make to the list through one variable will be seen through all other variables that refer to it.:
>>> rgb = ["Red", "Green", "Blue"]>>> rgba = rgb>>> id(rgb) == id(rgba) # they reference the same object True>>> rgba.append("Alph")>>> rgb ["Red", "Green", "Blue", "Alph"] ```
So I understand that the new list is a reference to the original list. But immediately then, the documentation states:
All slice operations return a new list containing the requested elements. This means that the following slice returns a shallow copy of the list:
>>> correct_rgba = rgba[:]>>> correct_rgba[-1] = "Alpha">>> correct_rgba ["Red", "Green", "Blue", "Alpha"]>>> rgba ["Red", "Green", "Blue", "Alph"] ```
So, if I understand correctly:
rgba[:]
is the slice operation- this makes a shallow copy of the original list
- it copies data from the original, into a new list that isn't a reference to the original
But:
- after reading about the difference between shallow and deep copies, I understand that shallow copies are references to their original, whilst deep copies are independent (unreferenced) copies
- in the example above, the documentation creates a deep copy but mentions it as a shallow copy?