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

Michael Selik mike at selik.org
Fri Jun 15 13:39:32 EDT 2018

On Fri, Jun 15, 2018 at 10:25 AM Mikhail V <mikhailwas at gmail.com> wrote:

> very uncommon to see standalone statements like: x.method()

Python has many such mutation methods. It sounds like you're judging the
frequency of code patterns across all languages instead of just Python.
Even then, I don't think that's true. All OO languages that come to mind
have that pattern frequently.

As for x+=1 it is implementation detail - historical idea of such operators
> was
> mutating, so at least visually its not like a returning expression.

Incorrect. The += operator was meant as an alias for ``x = x + 1``. The
fact that it mutates a list is somewhat of a surprise. Some other languages
make no distinction between mutation and reassignment. Perhaps you're
thinking of one of those other languages.

> and I am not sure about x.method() form - was it meant to hint to the user
> about anything? It seemed to me so when I started to learn Python, but its
> not.

Yes, it returns None to emphasize that it's a mutation. This is different
from the so-called "fluent" design pattern where all mutation methods also
return the original object, causing confusion about whether the return
value is a copy or not.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180615/4c0eeafc/attachment-0001.html>

More information about the Python-ideas mailing list