new_list = list(original_list)
new_dict = dict(original_dict)
new_set = set(original_set)
>>> x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> y = list(x) # делаем неглубокую копию
>>> x
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> y
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> x.append(['new list'])
>>> x
[[1, 2, 3], [4, 5, 6], [7, 8, 9], ['new list']]
>>> y
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> x[1][0] = 'Python'
>>> x
[[1, 2, 3], ['Python', 5, 6], [7, 8, 9], ['new list']]
>>> y
[[1, 2, 3], ['Python', 5, 6], [7, 8, 9]]
>>> import copy
>>> x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> z = copy.deepcopy(x)
>>> x
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> z
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> x[1][0] = 'Питон'
>>> x
[[1, 2, 3], ['Питон', 5, 6], [7, 8, 9]]
>>> z
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f'Point({self.x!r}, {self.y!r})'
>>> p = Point(15, 18)
>>> b = copy.copy(p)
>>> p
Point(15, 18)
>>> b
Point(15, 18)
>>> p is b
False
class Rectangle:
def __init__(self, topleft, bottomright):
self.topleft = topleft
self.bottomright = bottomright
def __repr__(self):
return (f'Rectangle({self.topleft!r}, '
f'{self.bottomright!r})')
rect = Rectangle(Point(0, 2), Point(7, 9))
nrect = copy.copy(rect)
>>> rect
Rectangle(Point(0, 2), Point(7, 9))
>>> nrect
Rectangle(Point(0, 2), Point(7, 9))
>>> rect is nrect
False
>>> rect.topleft.x = 33
>>> rect
Rectangle(Point(33, 2), Point(7, 9))
>>> nrect
Rectangle(Point(33, 2), Point(7, 9))
>>> drect = copy.deepcopy(srect)
>>> drect.topleft.x = 444
>>> drect
Rectangle(Point(444, 2), Point(7, 9))
>>> rect
Rectangle(Point(33, 2), Point(7, 9))
>>> nrect
Rectangle(Point(33, 2), Point(7, 9))