[Python-ideas] add fluent operator to everything
Steven D'Aprano
steve at pearwood.info
Wed Feb 20 21:48:08 EST 2019
On Thu, Feb 21, 2019 at 12:55:00PM +1100, Steven D'Aprano wrote:
> On Wed, Feb 20, 2019 at 10:24:25AM -0800, Bruce Leban wrote:
>
> > Here's a syntax that solves this using the new operators _:= and ,_
>
> Be careful about making such dogmatic statements.
Ah, I have just spotted your later explanation that you aren't referring
to *new operators* spelled as _:= and ,_ (as I understood from your
words) but as *existing syntax* that works today (at least in the most
up-to-date version of 3.8).
That pretty much makes all my previous objections to these "new
operators" redundant, as they aren't operators at all.
I'll point out that there's nothing special about the use of an
underscore here. Instead of your snippet:
_:= a ,_ .append(4) ,_ .sort()
we could write:
tmp := a, tmp.append(4), tmp.sort()
So this is hardly method chaining or a fluent interface. (Hence my
confusion -- I thought you were suggesting a way to implement a fluent
interface.)
If this were an Obfuscated Python competition, I'd congratulate you for
the nasty trick of putting whitespace in non-standard positions to
conceal what is going on. But in real code, that's only going to cause
confusion.
(But I think you acknowledge that this trick is not easy to read.)
But it's also sub-optimal code. Why introduce a temporary variable
instead of just writing this?
a.append(4), a.sort()
The only advantage of _ is that it is a one-character name. But there
are other one character names which are less mystifying:
a := some_long_identifier, a.append(4), a.sort()
But why create a tuple filled mostly with None? Python is slow enough as
it is without encouraging such anti-patterns.
a := some_long_identifier; a.append(4); a.sort()
Now no tuple is created, and doesn't need to be garbage collected.
But this is still not a fluent interface. Its just cramming multiple
statements into a single line. Which means you can't do this:
function(arg1, mylist.append(1).sort(), arg3)
--
Steven
More information about the Python-ideas
mailing list