[Python-ideas] A PEP on introducing variables on 'if' and 'while'

Nick Coghlan ncoghlan at gmail.com
Mon Jun 11 08:11:19 EDT 2018

On 11 June 2018 at 04:35, Juancarlo Añez <apalala at gmail.com> wrote:

> As the PEP author, that's your job.
> I started writing the PEP, and I found an interesting example:
>     if not (m := re.match(r'^(\d+)-(\d+)$', identifier):
>         raise ValueError('f{identifier} is not a valid identifier')
>     print(f'first part is {m.group(1)}')
>     print(f'first part is {m.group(2)}')
> That's fairly easy to understand, and not something that can be resolved
> with `as` if it's part of the `if` and `while` statement, rather than a
> different syntax for the `:=` semantics.

Yep, the "What about cases where you only want to capture part of the
conditional expression?" question is the rock on which every "only capture
the entire conditional expression" proposal has foundered.

PEP 572 arose from Chris deciding to take on the challenge of seriously
asking the question "Well, what if we *did* allow capturing of arbitrary
subexpressions with inline assignments?".


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180611/d61eca64/attachment.html>

More information about the Python-ideas mailing list