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

Juancarlo Añez apalala at gmail.com
Sun Jun 10 14:35:54 EDT 2018


> 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.

That one would have to be written as it is done now:

    m = re.match(r'^(\d+)-(\d+)$', identifier)

    if not m:

        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)}')


-- 
Juancarlo *Añez*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180610/bc2b9979/attachment.html>


More information about the Python-ideas mailing list