Semi-newbie, rolling my own __deepcopy__

Michael Spencer mahs at telcopartners.com
Thu Apr 21 20:27:29 EDT 2005


ladasky at my-deja.com wrote:
> I'm back...
[wondering why copy.deepcopy barfs on array instances]
> 

http://www.python.org/doc/2.3.3/lib/module-copy.html
deepcopy:
...
This version does not copy types like module, class, function, method, stack 
trace, stack frame, file, socket, window, *array*, or any similar types.
...

which you can see, by doing:
  >>> a = array("d",[random() for x in range(3)])
  >>> b = deepcopy(a)
  Traceback (most recent call last):
    File "<input>", line 1, in ?
    File "C:\Python24\lib\copy.py", line 172, in deepcopy
      y = copier(memo)
  TypeError: __deepcopy__() takes no arguments (1 given)

In any case there's is no difference between deep and shallow - copying an 
array, since it can contain only scalars, rather than compound objects:

http://www.python.org/doc/2.3.3/lib/module-copy.html
The difference between shallow and deep copying is only relevant for
compound objects (objects that contain other objects, like lists or
class instances).


array does have a __deepcopy__ method, albeit not compatible with copy.deepcopy. 
  You can use this to make the (shallow) copy.

  >>> b = a.__deepcopy__()


Michael




More information about the Python-list mailing list