[Python-ideas] PEP 572: Statement-Local Name Bindings

Ethan Furman ethan at stoneleaf.us
Wed Feb 28 10:25:55 EST 2018


On 02/28/2018 02:43 AM, Chris Angelico wrote:
> On Wed, Feb 28, 2018 at 8:04 PM, Robert Vanden Eynde wrote:

>> 3) "C problem that an equals sign in an expression can now create a name
>> binding, rather than performing a comparison." The "=" does variable
>> assignement already, and there is no grammar problem of "=" vs "==" because
>> the "with" keyword is used in the expression, therefore "with a == ..." is a
>> SyntaxError whereas "where a = ..." is alright (See grammar in thektulu
>> implemention of "where").
>
> Yes, but in Python, "=" does variable assignment *as a statement*. In
> C, you can do this:
>
> while (ch = getch())
>      do_something_with(ch)
>
> That's an assignment in an arbitrary condition, and that's a bug
> magnet. You cannot do that in Python. You cannot simply miss out one
> equals sign and have legal code that does what you don't want. With my
> proposed syntax, you'll be able to do this:
>
> while (getch() as ch):
>      ...
>
> There's no way that you could accidentally write this when you really
> wanted to compare against the character.

Given the current (posted) proposal, wouldn't 'ch' evaporate before the ':' and be unavailable in the 'while' body?

--
~Ethan~


More information about the Python-ideas mailing list