[Python-ideas] PEP 572: Assignment Expressions (post #4)

Nick Coghlan ncoghlan at gmail.com
Fri Apr 13 10:06:52 EDT 2018


On 13 April 2018 at 16:47, Chris Angelico <rosuav at gmail.com> wrote:
> Consider:
>
> pos = -1
> while pos := buffer.find(search_term, pos + 1) >= 0:
>     ...
>
> Once find() returns -1, the loop terminates. Should this need to be
> parenthesized?

I've certainly been assuming that cases like that would need to be written as:

    pos = -1
    while (pos := buffer.find(search_term, pos + 1)) >= 0:
       ...

I'd write the equivalent C while loop the same way:

    int pos = -1
    while ((pos = find(buffer, search_term, pos + 1)) >= 0):
       ...

The parentheses around the assignment in C are technically redundant,
but I consider finding the matching parenthesis to be straightforward
(especially with text editor assistance), while I consider figuring
out where the next lower precedence operator appears difficult (since
I don't have the C operand precedence table memorized, and there isn't
any simple way for my text editor to help me out).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list