On Sat, Mar 3, 2018 at 5:27 AM, Jelle Zijlstra <jelle.zijls...@gmail.com> wrote:
> 2018-03-02 7:03 GMT-08:00 Robert Vanden Eynde <robertv...@gmail.com>:
>> +1 on extracting the big win for "if" and "while" (the regex case is
>> wonderul). It would be see as an "extended if/while" rather than a general
>> statement assignation.
> I wonder if we could have a more limited change to the language that would
> allow only the as/while use cases. Specifically, that means we could do:
> while do_something() as x:
>     print(x)
> if re.match(r'.*', text) as match:
>     print(match.groups())
> Parentheses would no longer be necessary for syntactic ambiguity, and there
> is no real need for new scoping rules—we could just create new locals.
> This would alleviate some of the problems with the current proposal, such as
> complicated scoping rules and ugly syntax in comprehensions.

The trouble with this is that it's extremely restricted. It works for
the regex case, because re.match() has been specifically written to
function as either "return a match object" or "return true/false to
indicate a match", by guaranteeing to return a truthy object or None,
and nothing else. It would NOT work for anything where the bool() of
the desired object doesn't exactly match the loop's condition. For
instance, if do_something() returns None when it's done, but might
return an empty string during operation, the correct condition is
"while do_something() is not None" - and you can't capture the whole
condition any more. To be able to write "while (do_something() as x)
is not None:", you have to have the more general syntax that's used in
this PEP.

Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to