6.19. *Shallow and Deep Copies
Earlier in Section 3.5, we described how object assignments are simply object references. This means that when you create an object, then assign that object to another variable, Python does not copy the object. Instead, it copies only a reference to the object. For example:
>>> aList = [[78, 'pyramid'], [84, 'vulture'], [81, 'eye']] >>> anotherList = aList >>> aList [[78, 'pyramid'], [84, 'vulture'], [81, 'eye']] >>> >>> anotherList [[78, 'pyramid'], [84, 'vulture'], [81, 'eye']]
Above, a list of two elements is created and its reference assigned to aList. When aList is assigned to anotherList, the contents of the list reference by aList are not copied when anotherList is created. Rather, anotherList “copies” ...
Get Core Python Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.