[Numpy-discussion] Deserialization uncouples shared arrays
Hrvoje Niksic
hrvoje.niksic@avl....
Tue Sep 22 04:01:37 CDT 2009
Is it intended for deserialization to uncouple arrays that share a
common base? For example:
>>> import numpy, cPickle as p
>>> a = numpy.array([1, 2, 3]) # base array
>>> b = a[:] # view one
>>> b
array([1, 2, 3])
>>> c = a[::-1] # view two
>>> c
array([3, 2, 1])
>>> b.base is c.base
True
Arrays in b and c now share a common base, so changing the contents of
one affects the other:
>>> b[0] = 10
>>> b
array([10, 2, 3])
>>> c
array([ 3, 2, 10])
After serialization, the two arrays are effectively uncoupled, creating
a different situation than before serialization:
>>> d, e = p.loads(p.dumps((b, c), -1))
>>> d
array([10, 2, 3])
>>> e
array([ 3, 2, 10])
>>> d.base is e.base
False
>>> d[0] = 11
>>> d
array([11, 2, 3])
>>> e
array([ 3, 2, 10])
Is this behavior intentional, or is it an artifact of the
implementation? Can it be relied upon not to change in a future release?
More information about the NumPy-Discussion
mailing list