[Python-Dev] PEP 572: Assignment Expressions

Mike Miller python-dev at mgmiller.net
Fri Apr 20 12:50:29 EDT 2018


On 2018-04-19 23:52, Chris Angelico wrote:
> And are limited to conditions that check the truthiness/falsiness of
> the value you care about. So that works for re.match, but not for
> anything that might return -1 (a lot of C APIs do that, so if you're
> working with a thin wrapper, that might be all you get), and it'll
> encourage people to use this form when "is not None" would be more
> appropriate (setting up for a failure if ever the API returned a

 From the previously discussed code, it might look like this:

     while (file.get_next_token() as token) != -1:
         doc += token

Shouldn't be needed often, but I find it readable enough.

More generally, I've been -0 on this idea because I've come to appreciate 
Python's less-clever i.e. "dumb" loop syntax, and ":=" combined with 
assignment-expressions doesn't feel like Python at all but rather Pascal and C 
had a love-child, haha.

I could mildly support the "as" syntax however, since it is so darn readable and 
has analogues in other places.

That leaves what to do with "with".  Guess I missed the part in the discussion 
where we couldn't fit the syntax into it.  Would requiring parens here not work?

     with (expr() as name) as conman:
         pass

This should rarely be necessary or useful, correct?  Perhaps disallow for now.

On assignment to names/subscripts, just names sounds simpler for the first round.

Also the current "while" itself could be a bit simpler by making the expression 
optional and slightly less verbose:

     while:
         points = learner.get(static_hint)
         if not points:
            break


Thanks for the hard work,
-Mike


More information about the Python-Dev mailing list