On Thu, May 14, 2020 at 2:58 AM Steven D'Aprano <steve@pearwood.info> wrote:
On Mon, May 11, 2020 at 10:41:06AM -0700, Andrew Barnert via Python-ideas wrote:

> I think in general people will expect that a slice view on a sequence
> acts like “some kind of sequence”, not like the same kind they’re
> viewing—again, they won’t be surprised if you can’t insert into a
> slice of a list.

o_O

For nearly 30 years, We've been able to insert into a slice of a list.
I'm going to be *really* surprise if that stops working.

At this point, we're thinking a sequence view would be immutable anyway. Even for views on immutable objects. So That's a non-issue.

In numpy, there really isn't a view object at all -- there are simply numpy arrays, and any array *may* share the data block with another array. But they are both "proper" arrays.

In [20]: import numpy as np                                                                                    

In [21]: A = np.ones((5,))                                                                                    

In [22]: B = A[:]                                                                                              

In [23]: type(A)                                                                                              
Out[23]: numpy.ndarray

In [24]: type(B)                                                                                              
Out[24]: numpy.ndarray

There is a small distinction: in the above case, A "owns" the data block, but you can only tell if you poke into the flags:

In [27]: A.flags                                                                                              
Out[27]:
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [28]: B.flags                                                                                              
Out[28]:
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

And user code very rarely needs to care about that. That flag is mostly used to manege the memory, and prevent dangerous operations:

In [30]: A.resize((3,4))                                                                                      
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-cd4cee3d5a8d> in <module>
----> 1 A.resize((3,4))

ValueError: cannot resize an array that references or is referenced
by another array in this way.

There are good reasons for ndarrays being able to share data while still being mutable, but I don't think a "normal" Python seqence_view should be mutable -- it would lead to a lot of confusion.

-CHB

--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython