|
  
Source: ONLamp.com The copy module provides functions for duplicating objects using shallow or deep copy semantics.
Module: copyPurpose: Duplicate objects.Python Version: 1.4Description:The copy module includes 2 functions, copy() and deepcopy(), for duplicating existing objects.Shallow Copies:The shallow copy created by copy() is a new container populated with references to the contents of the original object. For example, a new list is constructed and the elements of the original list are appended to it.import copyclass MyClass: def __init__(self, name): self.name = name def __cmp__(self, other): return cmp(self.name, other.name)a = MyClass('a')l = [ a ]dup = copy.copy(l)print 'l :', lprint 'dup:', dupprint 'dup is l:', (dup is l)print 'dup == l:', (dup == l)print 'dup[0] is l[0]:', (dup[0] is l[0])print 'dup[0] == l[0]:', (dup[0] == l[0])For a shallow copy, the MyClass instance is not duplicated so the reference in the dup list is to the same object that is in the l list.$ python copy_shallow.pyl : []dup: []dup is l: Falsedup == l: Truedup[0] is l[0]: Truedup[0] == l[0]: TrueDeep Copies:The deep copy created by deepcopy() is a new container populated with copies of the contents of the original object. For example, a new list is constructed and the elements of the original list are copied, then the copies are appended to the new list.By replacing the call to copy() with deepcopy(), the difference becomes apparent.dup = copy.deepcopy(l)Notice that the first element of the list is no longer the same object reference, but the two objects still evaluate as being equal.$ python copy_deep.pyl : []dup: []dup is l: Falsedup == l: Truedup[0] is l[0]: Falsedup[0] == l[0]: TrueControlling Copy Behavior:It is possible to control how copies are made using the __copy__ and __deepcopy__ hooks.__copy__() is called without any arguments and should return a shallow copy of the object.__deepcopy__() is called with a memo dictionary, and should return a deep copy of the object. Any member attributes which need to be deep-copied should be passed to copy.deepcopy(), along with the memo dictionary, to control for recursion (see below).This example illustrates how the methods are called:import copyclass MyClass: def __init__(self, name): self.name = name def __cmp__(self, other): return cmp(self.name, other.name) def __copy__(self): print '__copy__()' return MyClass(self.name) def __deepcopy__(self, memo): print '__deepcopy__(%s)' % str(memo) return MyClass(copy.deepcopy(self.name, memo))a = MyClass('a')sc = copy.copy(a)dc = copy.deepcopy(a)$ python copy_hooks.py__copy__()__deepcopy__({})Recursion in Deep Copy:To avoid problems with duplicating recursive data structures, deepcopy() uses a dictionary to track objects which have already been copied. This dictionary is passed to the __deepcopy__() method so it can be used there as well.This example shows how an interconnected data structure such as a Digraph might assist with protecting against recursion by implementing a __deepcopy__() method. This particular example is just for illustration purposes, since the default implementation of deepcopy() already handles the recursion cases correctly.First some basic directed graph methods. A graph can be initialized with a name and a list of existing nodes to which it is connected. The addConnection() method is used to set up bi-directional connections. It is also used by the deepcopy operator.import copyimport pprintclass Graph: def __init__(self, name, connections): self.name = name self.connections = connections def addConnection(self, other): self.connections.append(other) def __repr__(self): return '%s) id=%s' % (self.name, id(self))The __deepcopy__() method prints messages to show how it is called, and manages the memo dictionary contents as needed. Instead of copying the connection list wholesale, it creates a new list and appends copies of the individual connections to it. That ensures that the memo dictionary is updated as each new node is duplicated, and avoids recursion issues or extra copies of nodes. As before, it returns the copied object when it is done. def __deepcopy__(self, memo): print print repr(self) not_there = [] existing = memo.get(self, not_there) if existing is not not_there: print ' ALREADY COPIED TO', repr(existing) return existing pprint.pprint(memo, indent=4, width=40) dup = Graph(copy.deepcopy(self.name, memo), []) print ' COPYING TO', repr(dup) memo[self] = dup for c in self.connections: dup.addConnection(copy.deepcopy(c, memo)) return dupNext we can set up a graph with a nodes root, a, and b. The edges are a-root, b-a, b-root, root-a, root-b.root = Graph('root', [])a = Graph('a', [root])b = Graph('b', [a, root])root.addConnection(a)root.addConnection(b)dup = copy.deepcopy(root)When the root node is copied, we see:$ python copy_recursion.py{} COPYING TO { : , 488896: 'root', 497392: ['root']} COPYING TO ALREADY COPIED TO { : , : , 237120: 'a', 488896: 'root', 497392: [ 'root', 'a', , ], 508592: , 510512: } COPYING TO Notice that the second time the root node is encountered, while the a node is being copied, the recursion is detected and the existing copy is used instead of a new one.References:Python Module of the Week HomeDownload Sample CodeTechnorati Tags:python, PyMOTW
|