[Numpy-discussion] who owns the data?

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Nov 30 16:21:00 EST 2011

On Wed, Nov 30, 2011 at 4:00 PM, Robert Kern <robert.kern at gmail.com> wrote:
> On Wed, Nov 30, 2011 at 20:30,  <josef.pktd at gmail.com> wrote:
>> just a basic question (since I haven't looked at this in some time)
>> I'm creating a structured array in a function. However, I want to
>> return the array with just a simple dtype
>> uni = uni.view(dt).reshape(-1, ncols)
>> return uni
>> the returned uni has owndata=False. Who owns the data, since the
>> underlying, original array went out of scope?
> Every time you make a view through .view(), slicing, .T, certain
> restricted .reshape() calls , etc. a reference to the original object
> is stored on the view. Consequently, the original object does not get
> garbage collected until all of the views go away too. Making view of a
> view just adds another link in the chain. In your example, the
> original object that was assigned to `uni` before that last assignment
> statement was executed maintains ownership of the memory. The new
> ndarray object that gets assigned to `uni` for the return statement
> refers to the temporary ndarray returned by .view() which in turn
> refers to the original `uni` array which owns the actual memory.

Thanks for the explanation.

There where cases on the mailing list where views created problem, so
I just thought of trying to own the data, but I don't think it's
really relevant.

>> 2)
>> uni.dtype = dt
>> uni.reshape(-1, ncols)
>> return uni
>> this works and uni owns the data.
> uni.reshape() doesn't reshape `uni` inplace, though. It is possible
> that your `uni` array wasn't contiguous to begin with. In all of the
> cases that your first example would have owndata=False, this one
> should too.

this bug happened to me a few times now. I found it but only checked
the flags before fixing it.

Since reshape again creates a view, the next step is to assign to shape

uni.shape = (uni.size//ncols, ncols)

but that starts to look like too much inplace modifications just to avoid a view



>> I'm only worried whether assigning
>> to dtype directly is not a dangerous thing to do.
> It's no worse than .view(dt). The same kind of checking goes on in both places.
> --
> Robert Kern
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>   -- Umberto Eco
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list