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

Rob Cliffe rob.cliffe at btinternet.com
Tue Feb 27 23:38:56 EST 2018


On 27/02/2018 22:27, Chris Angelico wrote:
> This is a suggestion that comes up periodically here or on python-dev.
> This proposal introduces a way to bind a temporary name to the value
> of an expression, which can then be used elsewhere in the current
> statement.
>
>
Hm, apologies.  This is in complete contrast to my previous post, where 
I was pretty enthusiastic about Chris's PEP.  But I can't resist sharing 
these thoughts ...

There was some vague uneasiness at the back of my mind, which I think I 
have finally pinned down.  Consider Chris's example:
          # Using a statement-local name
         stuff = [[(f(x) as y), y] for x in range(5)]
I think what bothered me was the *asymmetry* between the two uses of the 
calculated value of f(x).  It is not obvious at first glance that
     [(f(x) as y), y]
defines a 2-element list where the 2 elements are the *same*. Contrast 
something like (exact syntax bike-sheddable)
           stuff = [ (with f(x) as y: [y,y])  for x in range(5)]
or
             stuff = [ (y,y] with f(x) as y) for x in range(5)]
This also has the advantage (if it is? I think probably it is) that the 
scope of the temporary variable ("y" here) can be limited to inside the 
parentheses of the "with" sub-expression.
And that it is not dependent on Python's evaluation order.
Ir gives the programmer explicit control over the scope, which might 
conceivably be an advantage in more complicated expressions.

Sorry if this is re-hashing a suggestion that has been made before, as 
it probably is.  It just struck me as ... I don't know ... cleaner somehow.
Regards
Rob Cliffe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180228/bbd01807/attachment.html>


More information about the Python-ideas mailing list