[Numpy-discussion] numarray __copy__ and __deepcopy__
Fernando Perez
fperez at colorado.edu
Fri Nov 7 13:37:17 CST 2003
Tim Hochberg wrote:
> It appears that numarray.NumArray does not supply __copy__ or
> __deepcopy__ methods and as a result copy.copy and copy.deepcopy do not
> work correctly. It appears that adding "__copy__ = copy" to class
> NumArray is suffcient for copy, __deepcopy__ appears to need something
> more. Sadly, I don't have time to investigate this further right now.
As far as I understand, this isn't really necessary with Numeric/Numarray,
because the copy() method in a sense always guarantees a 'deep copy'. Even
when you make assignments to slices of an array, the issue of nested
structures which for python lists/dicts requires deepcopy() just does not
arise. A simple illustration:
In [1]: a=arange(10)
In [2]: b=arange(10,20)
In [3]: c=arange(20,30)
In [4]: d=zeros(30)
In [5]: d[0:10] = a
In [6]: d[10:20] = b
In [7]: d[20:30] = c
In [8]: a
Out[8]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [9]: b
Out[9]: array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
In [10]: c
Out[10]: array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
In [11]: d
Out[11]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
In [12]: b[:]=99
In [13]: b
Out[13]: array([99, 99, 99, 99, 99, 99, 99, 99, 99, 99])
In [14]: d
Out[14]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
Perhaps I'm missing some usage case, but I've always just used ARR.copy() when
I've needed a 'full copy' of an array. This guarantees that the returned
array is contiguous (has .flat) and a standalone copy of the data in ARR,
regardless of the contiguity properties of ARR.
HTH.
Cheers,
f
ps: my experience is actually from Numeric, I don't know if Numarray differs
in its copy() behavior.
More information about the Numpy-discussion
mailing list