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

George Leslie-Waksman waksman at gmail.com
Thu May 24 12:53:50 EDT 2018


I worry about the use of "for" because it will come up in contexts where
"for" already has other meanings. In the case of the example list
comprehension, the word "for" is being used to mean two entirely different
things in a single expression, that seems rather precarious to me.

I prefer "with" if we're looking for keywords to reuse. This feels fairly
clean to me:

[(x, y, x/y) for x in data with y = f(x) if y]

if m with m = pattern.search(data):
    ...

while m with m = pattern.search(remaining_data):
    ...

Of course, "with" is not without problems and I'm not so happy about being
able to:

with open(a) as b with a = filename:
    ...

--George

On Thu, May 24, 2018 at 9:33 AM Robert Vanden Eynde <robertve92 at gmail.com>
wrote:

> It was a long time ago I couldn't easily find the post but that's alright,
> you refreshed the idea :)
>
> Let's see what others think of for x =
>
> I also remembered some languages (like lua) use for x = range (5)
> interchangeably with for x in range (5) and guido said it will never make
> such a thing, for .. in being the iteration.
>
> Le jeu. 24 mai 2018 à 18:22, Alexander Belopolsky <
> alexander.belopolsky at gmail.com> a écrit :
>
>>
>>
>> On Thu, May 24, 2018 at 12:04 PM Robert Vanden Eynde <
>> robertve92 at gmail.com> wrote:
>>
>> > This idea was mentioned (by me) at a time yes, but wasn't written in
>> the document.
>>
>> Can you point me to a specific post?  There were so may that I must have
>> missed that one.
>>
>> > 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.
>>
>> I don't see how that can be a problem.  From the grammar point of view,
>> "for" in "for var = <expr>" may still be seen as introducing a "loop", but
>> when "=" is seen in place of "in", the compiler will resize that the "loop"
>> is one a single value and emit efficient code for it.  At the AST level,
>>   "for var = <expr>"  will look exactly the same as  "for var in <expr>"
>> only with an "=" instead of "in".
>>
>> > And also, it might confuse people because 'for' is for iteration.
>>
>> I think I addressed this in my previous post.  Yes, for people with a
>> C/C++ background, "for" may be too strongly associated with loops, but in
>> mathematical sense, it seems clear that "for var in a set" means iteration
>> over a set, while "for var = expression" means binding to a single value.
>>
> _______________________________________________
> 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/6aa33615/attachment.html>


More information about the Python-ideas mailing list