[Numpy-discussion] Pickling and initializing

Anne Archibald peridot.faceted at gmail.com
Mon Mar 3 19:28:59 EST 2008


On 03/03/2008, Dinesh B Vadhia <dineshbvadhia at hotmail.com> wrote:

> When you pickle a numpy/scipy matrix does it have to be initialized by
> another program?  For example:

Most python objects do not need to be initialized. You just call a
function that makes the one you want:

>>> l = range(10)

This makes a list of length 10. You can now manipulate it, adding
elements or what have you.

Arrays are no different. (You use matrices in your example - the only
difference is that the multiplication operator behaves differently,
and you often need to use asmatrix to convert them back to arrays. I
never use them, even when I have to do some linear algebra.) You
simply call a function that makes the array you want:

>>> a = arange(10)

You can change its contents, but it's not really sensible to say that
arrays must be initialized before use. The function empty() is kind of
a peculiar aberration - it's for those rare cases when you end up
reassigning all the values in the array, and zeros() is too slow. (For
debugging it'd be nice to have NaNs()...)

Perhaps you are thinking of statically-typed languages, where
variables must be initialized? In python variables do not have type,
so variables holding arrays are no different from variables holding
strings, integers, file objects, or whatever. Using a python variable
before it has been assigned a value does indeed raise an exception;
all that is needed is to assign a value to it.

Unpickling reads a file and constructs a "new" array from the data in
that file. The array value is returned; one often assigns this value
to a variable. The values in the array are filled in by the pickling
function. It is not possible to make the unpickler store its data in a
preallocated array.

Anne



More information about the NumPy-Discussion mailing list