On Mon, Jun 18, 2018 at 12:56 PM Mikhail V <mikhailwas@gmail.com> wrote:
Numpy arrays have also append() and insert() methods,
In [2]: np.arange(1).append(2) AttributeError: 'numpy.ndarray' object has no attribute 'append' In [3]: np.arange(1).insert AttributeError: 'numpy.ndarray' object has no attribute 'insert' So one syntax generalization possible is towards insert() method.
Why would you even want to encourage inserting into a list? It's slow and should be *discouraged*. Although IMO it could be made into nice syntax only by introducing
some symbol into the index notation. For example:
L[] = item append(item) L[^] = item ? appendleft(item) L[^0] = item insert (0, item) ... L[^i] = item insert(i, x)
Note, I am not responsible for the adequacy of the technical part - since it may be not plausible technically.
Indeed, changing the syntax to add a special meaning to caret operator inside of index assignment is a *major* change. Not worthwhile for such dubious benefit. On Mon, Jun 18, 2018 at 1:08 PM Joao S. O. Bueno <jsbueno@python.org.br> wrote:
MutableSequence protocol to define "<<" as "append"
No one has demonstrated with any realistic examples why code would look better with ``a <<= b`` instead of ``a.append(b)``. Perhaps a language in its infancy could toy with new spellings, but Python is old now and should be more cautious with change. I know that at least Brython used the "<<=" enhanced assignement
operator to manipulate HTML DOM, and it often results in quite compact code when compared with javascript-equivalente manipulations.
That sounds great for a dedicated HTML DOM manipulation tool, but not for the core list type. On Sun, Jun 17, 2018 at 6:12 PM Chris Angelico <rosuav@gmail.com> wrote:
Actually, maybe the problem here is that there's no easy way to represent "-0" in a slice.
items[:0] = ["shim"] # insert at beginning
items[-0:] # failed parallel
The fact that signed 2's complement integers don't support negative zero does cause some problems for both indexing and slicing. However, to me it seems this is a fundamental human problem with zero. Thus the ever-present off-by-one error. I can't think of a solution that doesn't cause more harm than good.