[Numpy-discussion] Checking for views (was: Should arr.diagonal() return a copy or aview?)

Nathaniel Smith njs at pobox.com
Thu May 24 13:59:38 EDT 2012


On Thu, May 24, 2012 at 6:07 PM, Larsen, Brian A <balarsen at lanl.gov> wrote:
> This is the stack overflow discussion mentioned.
>
> http://stackoverflow.com/questions/9164269/can-you-tell-if-an-array-is-a-view-of-another
>
> I basically implemented the answer from SO.  I feel like the "is" gives you
> a good handle on things since to be true they are actually the same location
> in memory.

If using the current development version of numpy, that answer is
actually wrong... if you do
  a = np.arange(10)
  b = a.view()
  c = b.view()
then in the development version, c.base is a, not b. This is the
source of some contention and confusion right now...:
  https://github.com/numpy/numpy/pull/280#issuecomment-5888154

In any case, if "b.base is a" is True, then you can be pretty certain
that b and a share memory, but if it is False, it doesn't tell you
much at all. AFAICT np.may_share_memory would be strictly more useful.

-- Nathaniel



More information about the NumPy-Discussion mailing list