[Python-ideas] add fluent operator to everything

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Tue Feb 19 22:10:16 EST 2019


Brett Cannon writes:
 > On Tue, Feb 19, 2019 at 6:23 AM Jimmy Girardet <ijkl at netc.fr> wrote:

 > > I would be happy to have
 > >
 > > >>> [1,2,3].append(4)::sort()::max() +1
 > >
 > > It makes things very easy to read: first create list, then append 4,
 > > then sort, then get the max.

(Responding to the OP)

In most cases, where one doesn't care about performance, one can rewrite
as

    max(a := sorted([1, 2, 3] + [4])) + 1

This also tells you something about readability.  I find the
"dot-chaining" style easier to analyze than the nested function call
style, because the methods are applied left-to-right and few inline
operators are interpreted with right associativity -- note that this
is a runtime decision!  Where I don't care about analysis at this
site, I define a function.

If I do care about performance, writing it out line by line helps to
emphasize that and to optimize well.

I think given the current design with its long history, it would be
more useful to identify pain points where functions like sorted()
don't exist.

 > Also realize that design is on purpose so that mutating method
 > calls on lists do not return themselves to get the point across
 > that the mutation was in-place and not in fact a new list.

I have come to think that this is suboptimal.  Now I would like to
experiment with a Python-like language where methods *always* return
self, and where returning something else makes sense, Python-oid
should provide a function that does that.  Of course there would be an
exception for dunders.

It's not irritating enough to actually do this, though. :-)

Steve


More information about the Python-ideas mailing list