[Python-ideas] Alternative spelling for list.append()

Michael Selik mike at selik.org
Mon Jun 18 16:43:08 EDT 2018


On Mon, Jun 18, 2018 at 12:56 PM Mikhail V <mikhailwas at 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 at 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 at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180618/a87b3653/attachment-0001.html>


More information about the Python-ideas mailing list