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

Steven D'Aprano steve at pearwood.info
Sun Jun 17 21:25:03 EDT 2018


On Sun, Jun 17, 2018 at 08:01:09PM +0300, Mikhail V wrote:

> The idea is to introduce new syntax for the list.append() method.

Before trying to justify any specific syntax, you need to justify the 
idea of using syntax in the first place.


> Motivation
> -----------
> 
> 1. Assignment form reduces text amount in statements and makes the right-hand
> part, namely the item, clean of additional brackets, which is improtant
> especially by more complex items which can also contain brackets or quotes.

Reducing human-readable words like "append" in favour of cryptic symbols 
like "[] =" is not a motivation that I agree with. I think this syntax 
will make a simple statement like 

    mylist.append(x)

harder to read, harder to teach, and harder to get right:

    mylist[] = x

Using a *named method* is a Good Thing. Replacing named methods with 
syntax needs to be carefully justified, not just assumed that our motive 
should be to reduce the number of words.

On the contrary: we should be trying to keep the amount of symbols 
fairly small. Not zero, but each new symbol and each new syntactic form 
using symbols needs to be justified.

Why should this be syntax if a method will work?

> For example:
> 
>     mylist.append([[foo, bar], [] ])
> 
> Could be written as:
> 
>     mylist[] = [[foo, bar], [] ]
> 
> Which preserves the original item form and has generally more balanced look.

The original version preserves the original item form too, and I 
disagree that the replacement looks "more balanced".



> 2. Method form has one general issue, especially by longer variable names.
> Example from https://docs.python.org/3/tutorial/datastructures.html
> 
>     ...
>     for row in matrix:
>         transposed_row.append (row[i])
>     transposed.append (transposed_row)

There's nothing wrong with that example except you have stuck a space 
between the method name and the opening parenthises.


> It becomes hard to read because of lack of spacing between variable names
> and list names.

That's your opinion. I think that's unjustified, but even if it were 
justified, there are *hundreds* or *thousands* of method calls and 
function calls where you might make the same claim. Should we invent 
syntax for every single method call?


> 3. Item appending is very frequent operation.

Not that frequent.


> In current syntax, extend() method has dedicated syntax.
> 
>     mylist1 += mylist2

No, that is wrong. The += syntax applies to *any* type, *any* value 
which supports the plus operator. It is NOT dedicated syntax for the 
extend syntax.


> One of important aspects of proposal is that it should discourage usage of +=
> for appending an item. Namely the idea is to discourage this form :
> 
>     mylist += [item]
> 
> Because it is clunky and may cause additional confusion.

That should be discouraged because it uses special syntax instead of a 
self-explanatory method call. 

Again, I disagree with both this motivation and the supposed solution.


> E.g. adding brackets is often used and understood as operation of increasing
> dimension of a list

Can you provide an example of somebody who made this error, or did you 
just make it up?

Mikhail, I disagree with your motivation, and I disagree with your 
supposed solution. As far as I am concerned:

- I disagree that list.append method is a problem to be fixed;

- but even if it were a problem that needs fixing, your suggested
  syntax would be *worse* than the problem.



-- 
Steve


More information about the Python-ideas mailing list