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

Nick Coghlan ncoghlan at gmail.com
Sat Mar 24 04:35:32 EDT 2018


On 24 March 2018 at 13:15, Chris Angelico <rosuav at gmail.com> wrote:

> On Sat, Mar 24, 2018 at 2:09 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> > I'm certainly hoping PEP 572 is rejected so we can have a follow-up PEP
> that
> > only deals with the assignment-as-expression portion.
> >
> > No offense intended, Chris!  :)  In fact, maybe you could write that one
> > too, and then have an accepted PEP to your name?  ;)
> >
>
> Okay, maybe I *do* need to split them. There are other uses for
> statement-local names, so they're both potentially viable. Would that
> be the best way forward?
>
> If I do, sooner would be better than later - I'd want to grab PEP 573
> while it's still available.
>

Inline assignment (whether using the "expr as name" syntax or a new
dedicated "x := y" one) certainly comes up often enough that it would be
worth writing up independently of any of the statement local scoping
proposals.

One benefit of " x := y" is that it would avoid ambiguity in the with
statement and exception handling cases:

    with cm := cm_expr as enter_result:
        ...

    try:
        ...
    except exc_filter := exceptions_to_catch as caught_exc:
        ...

In comprehensions and generator expressions, we'd need to explain why
inline assignments in the outermost iterator expression leak but those in
filter expressions, inner iterator expressions, and result expressions
don't.

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.

That way, there'd technically be overlap between the two in the simple case
of assigning to a single name, but in other circumstances, it would be
obvious which one you needed to use.

Cheers,
Nick.

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 :)


-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180324/101e62e4/attachment-0001.html>


More information about the Python-ideas mailing list