[Python-Dev] Examples for PEP 572

Devin Jeanpierre jeanpierreda at gmail.com
Wed Jul 4 04:03:02 EDT 2018

On Wed, Jul 4, 2018 at 12:26 AM Nathaniel Smith <njs at pobox.com> wrote:
> The only cases that seem potentially valuable to me are the ones that
> are literally the form 'if <name> := <expr>` and 'while <name> :=
> <expr>'. (I suspect these are the only cases that I would allow in
> code that I maintain.) The PEP does briefly discuss the alternative
> proposal of restricting to just these two cases, but rejects it
> because it would rule out code like 'if (<name> := <expr>)
> <comparison> <expr>'. But those are exactly the cases that I want to
> rule out, so that seems like a plus to me :-).

The PEP doesn't talk about it, but FWIW, Go and C++17 if statements
allow you to put arbitrary simple statements in the suite header, and
by doing that, solves all the issues you and the PEP mentioned. In
Python it'd look like this:

if x = expr(); x < 0:

(and presumably, the same for while: "while line = f.readline(); line: ...")

-- Devin

More information about the Python-Dev mailing list