# [Numpy-discussion] Determining the 'viewness' of an array, and flags.owndata confusion

Nathaniel Smith njs@pobox....
Tue Feb 28 17:31:08 CST 2012

```On Tue, Feb 28, 2012 at 11:01 PM, Kurt Smith <kwmsmith@gmail.com> wrote:
> For an arbitrary numpy array 'a', what does 'a.flags.owndata' indicate?

I think what it really indicates is whether a's destructor should call
free() on a's data pointer.

> I originally thought that owndata is False iff 'a' is a view.  But
> that is incorrect.
>
> Consider the following:
>
> In [119]: a = np.zeros((3,3))
>
> In [120]: a.flags.owndata  # should be True; zeros() creates and
> returns a non-view array.
> Out[120]: True
>
> In [121]: a_view1 = a[2:, :]
>
> In [122]: a_view1.flags.owndata   # expected to be False
> Out[122]: False
>
> In [123]: a_fancy1 = a[[0,1], :]
>
> In [124]: a_fancy1.flags.owndata  # expected to be True, a_fancy1 is a
> fancy-indexed array.
> Out[124]: True
>
> In [125]: a_fancy2 = a[:, [0,1]]
>
> In [126]: a_fancy2.flags.owndata # expected to be True, a_fancy2 is a
> fancy-indexed array.
> Out[126]: False
>
> So when I query an array's flags.owndata, what is it telling me?  What
> I want to know is whether an array is a view or not.  If flags.owndata
> has nothing to do with the 'viewness' of an array, how would I
> determine if an array is a view?
>
> In the previous example, a_fancy2 does not own its data, as indicated
> by 'owndata' being False.  But when I modify a_fancy2, 'a' is not
> modified, as expected, but contrary to what 'owndata' would seem to
> indicate.

It looks like the fancy indexing code in this case actually creates a
new array, and then instead of returning it directly, returns a
(reshaped) view on this new array. If you look at a_fancy2.base,
you'll see this new array.

So: a_fancy2 *is* a view... it's just not a view of 'a'. It's a view
of this other array.

> If I cannot use flags.owndata, what is a reliable way to determine
> whether or not an array is a view?

owndata *is* a reliable way to determine whether or not an array is a
view; it just turns out that this is not a very useful question to