The copy module includes two functions, copy() and deepcopy(), for duplicating existing objects.

Shallow Copy


In [3]:
import copy
import functools


@functools.total_ordering
class MyClass:

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name == other.name

    def __gt__(self, other):
        return self.name > other.name


a = MyClass('a')
my_list = [a]
dup = copy.copy(my_list)

print('             my_list:', my_list)
print('                 dup:', dup)
print('      dup is my_list:', (dup is my_list))
print('      dup == my_list:', (dup == my_list))
print('dup[0] is my_list[0]:', (dup[0] is my_list[0]))
print('dup[0] == my_list[0]:', (dup[0] == my_list[0]))
print('id(dup[0])==id(dup[0])', (id(dup[0])==id(dup[0])))


             my_list: [<__main__.MyClass object at 0x108e036a0>]
                 dup: [<__main__.MyClass object at 0x108e036a0>]
      dup is my_list: False
      dup == my_list: True
dup[0] is my_list[0]: True
dup[0] == my_list[0]: True
id(dup[0])==id(dup[0]) True

Deep Copy


In [5]:
import copy
import functools


@functools.total_ordering
class MyClass:

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name == other.name

    def __gt__(self, other):
        return self.name > other.name


a = MyClass('a')
my_list = [a]
dup = copy.deepcopy(my_list)

print('             my_list:', my_list)
print('                 dup:', dup)
print('      dup is my_list:', (dup is my_list))
print('      dup == my_list:', (dup == my_list))
print('dup[0] is my_list[0]:', (dup[0] is my_list[0]))
print('dup[0] == my_list[0]:', (dup[0] == my_list[0]))


             my_list: [<__main__.MyClass object at 0x108e10c88>]
                 dup: [<__main__.MyClass object at 0x108e10d30>]
      dup is my_list: False
      dup == my_list: True
dup[0] is my_list[0]: False
dup[0] == my_list[0]: True

Customizing Copy Behavior


In [6]:
import copy
import functools


@functools.total_ordering
class MyClass:

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name == other.name

    def __gt__(self, other):
        return self.name > other.name

    def __copy__(self):
        print('__copy__()')
        return MyClass(self.name)

    def __deepcopy__(self, memo):
        print('__deepcopy__({})'.format(memo))
        return MyClass(copy.deepcopy(self.name, memo))


a = MyClass('a')

sc = copy.copy(a)
dc = copy.deepcopy(a)


__copy__()
__deepcopy__({})

Recursion in Deep Copy


In [7]:
import copy


class Graph:

    def __init__(self, name, connections):
        self.name = name
        self.connections = connections

    def add_connection(self, other):
        self.connections.append(other)

    def __repr__(self):
        return 'Graph(name={}, id={})'.format(
            self.name, id(self))

    def __deepcopy__(self, memo):
        print('\nCalling __deepcopy__ for {!r}'.format(self))
        if self in memo:
            existing = memo.get(self)
            print('  Already copied to {!r}'.format(existing))
            return existing
        print('  Memo dictionary:')
        if memo:
            for k, v in memo.items():
                print('    {}: {}'.format(k, v))
        else:
            print('    (empty)')
        dup = Graph(copy.deepcopy(self.name, memo), [])
        print('  Copying to new object {}'.format(dup))
        memo[self] = dup
        for c in self.connections:
            dup.add_connection(copy.deepcopy(c, memo))
        return dup


root = Graph('root', [])
a = Graph('a', [root])
b = Graph('b', [a, root])
root.add_connection(a)
root.add_connection(b)

dup = copy.deepcopy(root)


Calling __deepcopy__ for Graph(name=root, id=4443932488)
  Memo dictionary:
    (empty)
  Copying to new object Graph(name=root, id=4443878792)

Calling __deepcopy__ for Graph(name=a, id=4443932376)
  Memo dictionary:
    Graph(name=root, id=4443932488): Graph(name=root, id=4443878792)
  Copying to new object Graph(name=a, id=4443878904)

Calling __deepcopy__ for Graph(name=root, id=4443932488)
  Already copied to Graph(name=root, id=4443878792)

Calling __deepcopy__ for Graph(name=b, id=4441866080)
  Memo dictionary:
    Graph(name=root, id=4443932488): Graph(name=root, id=4443878792)
    Graph(name=a, id=4443932376): Graph(name=a, id=4443878904)
    4443932488: Graph(name=root, id=4443878792)
    4443904328: [Graph(name=root, id=4443932488), Graph(name=a, id=4443932376)]
    4443932376: Graph(name=a, id=4443878904)
  Copying to new object Graph(name=b, id=4443880696)