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

Christoph Groth christoph at grothesque.org
Sun Mar 25 05:55:59 EDT 2018


Nick Coghlan wrote:

> It would also result in two different ways to handle traditional
> assignments:
> 
>     x = expr
>     x := expr
> 
> Perhaps ":=" could be explicitly restricted to only single names on
> the LHS, without any of the clever unpacking features of full
> assignment statements? Unlike full assignment statements, assignment
> expressions also wouldn't have anywhere to put a type annotation.

Why would "=" have to be kept for anything else but backwards
compatibility and saving keystrokes?

The expression

var: annotation := value

could assign a value to a name and it would have the corresponding
value.

I guess that it's problematic for the language grammar to allow things
like

for i in (var: annotation := value):
    print(i)

In that case such constructions could be declared illegal.

> P.S. Pascal was one of the first languages I used to write a
> non-trivial application (a game of Battleships), so I'm predisposed
> towards liking ":=" as an assignment operator :)

Having an assignment operator that is optically distinct from '=' is IMO
a strong point of Pascal.

For Python, an even better assignment operator would be IMO "<-", but
this is not possible without breaking backwards compatibility, and also
it's a bit difficult to type on an English keyboard.


More information about the Python-ideas mailing list