[Python-ideas] Operator for inserting an element into a list

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jun 15 21:06:45 EDT 2018


Michael Selik wrote:
> The += operator was meant as an alias for ``x = x + 1``. The 
> fact that it mutates a list is somewhat of a surprise.

That's very much a matter of opinion. For every person who
thinks this is a surprise, you can find another that thinks
it's obvious that += should mutate a list, and is surprised
by the fact that it works on immutable types at all.

> Some other 
> languages make no distinction between mutation and reassignment. Perhaps 
> you're thinking of one of those other languages.

Most languages, actually. I'm not aware of any other
language with a += operator that has this dual interpretation
of its meaning -- which is probably why almost everyone gets
surprised by at least one of its meanings. :-)

Maybe we should call it the "Spanish Inquisition operator".

-- 
Greg


More information about the Python-ideas mailing list