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.

Language design is not a matter of picking random syntax and claiming 
that it solves the problem -- especially when it doesn't solve the 
problem.

Putting aside the aethestics of your suggestions (I think they are as 
ugly as sin), both of them are currently legal code, since a single 
underscore _ is a legal identifier. Starting in 3.8, _:= is the left 
hand side of an assignment expression assigning to the variable "_", so 
your suggestion:

    _:=    a

already has meaning in Python. That's a new feature, and you can be 
forgiven for perhaps not knowing about it. But the second suggestion is 
a very old feature, going back to Python 1 days. ,_ is part of a tuple 
or argument list including a variable called "_".

You probably could have discovered this for yourself by trying something 
like this in the interactive interpreter:


py> x = []
py> x,_.append(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'

The actual result you would get (an exception, or a tuple) will depend 
on the history of your current session, but whatever result you get it 
will show that it is already legal syntax. That means that there's going 
to be an ambiguity between your hoped-for semantics and the existing 
semantics:

    _:= a ,_.b

would mean a tuple with two items:

    _:= a

    _.b

rather than a chained method call as you hoped.


> a = [1,2,3]
> (    _:=    a    ,_    .append(4)    ,_    .sort()    )
> 
> 
> Personally, I find this a bit harder to read on one line and would break it
> up like this:
> 
> (_:=    a
> ,_      .append(4)
> ,_ .    .sort()
> )


That's currently legal code which assigns a to _ then builds a tuple (or 
at least attempts to build a tuple) consisting of:

    a
    None
    None

after calling a.append and a.sort.


But there's a deeper problem with this entire concept, regardless of 
syntax, one which to my knowledge nobody has mentioned yet: it simply 
isn't compatible with the way operators work in Python at the moment. 
More on this in another post (coming soon).


-- 
Steven
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to