[Python-ideas] Reuse "for" to express "given"

Robert Vanden Eynde robertve92 at gmail.com
Thu May 24 12:04:34 EDT 2018


This idea was mentioned (by me) at a time yes, but wasn't written in the
document.

I think one of the thing was that it would make the grammar non LL1 because
when seeing the token "for" in a list comprehension it wouldn't know in
advance if it's the loop or the assignment.

And also, it might confuse people because 'for' is for iteration.



Le jeu. 24 mai 2018 à 17:54, Alexander Belopolsky <
alexander.belopolsky at gmail.com> a écrit :

> I have read most of the PEP 572 related threads, but I don't think I've
> seen this idea.  As many other people mentioned, Python already allows a
> trick to introduce local bindings in generator expressions and list
> comprehensions.  This can be achieved by adding a "for var in [<expr>]"
> clause.  I propose to make "for var = <expr>" have the same effect as "for
> var in [<expr>]".
>
> Note that in the case of filters, the order will be different from that in
> the original "given" proposal: instead of
>
> [(x, y, x/y) for x in data if y given y = f(x)]
>
> we will write
>
> [(x, y, x/y) for x in data for y = f(x) if y]
>
> The use of "for" in place of "given" in the if statements does not look
> too bad:
>
>    if m for m = pattern.search(data):
>         ...
>     elif m for m = other_pattern.search(data)):
>         ...
>     else:
>         ...
>
> I have to admit that
>
> while m for m = pattern.search(remaining_data):
>         ...
>
> looks strange at first because both while and for are strongly associated
> with loops, but if you read "for var = <expr>" as "for var equals
> expression", the difference between that and "for var in (iterable)
> expression" becomes apparent.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180524/a99c33ac/attachment.html>


More information about the Python-ideas mailing list