[Python-ideas] PEP 572: Statement-Local Name Bindings, take three!

BrenBarn brenbarn at brenbarn.net
Tue Mar 27 22:17:18 EDT 2018


On 03/23/2018 03:01 AM, Chris Angelico wrote:
> Apologies for letting this languish; life has an annoying habit of
> getting in the way now and then.

	My simple response to all of this is that it's not worth it.  Each new 
example convinces me more and more that in almost every case, sublocal 
assignments DECREASE readability as long as they occur inline.  If the 
statement is very simple, the sublocal assignments make it complex.  If 
it is complex, they do not aid in seeing parallelism between different 
pieces that reuse the same value, because the sublocal assignment itself 
creates an asymmetry.

	The only alternatives that I see as increasing readability are the 
"rejected" alternatives in which the sublocal assignment is moved "out 
of order" so that all references to it look the same and are separated 
from the (single) assignment --- i.e., the variants of the form "x = a+b 
with a='foo', b='bar'".

	(I think someone already mentioned this, but these variants, even if 
rejected, probably shouldn't be placed under the header of 
"special-casing comprehensions".  Extracting the assignment to a 
with-clause makes sense outside of comprehensions too.  It would make 
more sense to label them as "out of order" or "non-inline" or perhaps 
"cleft assignment", by analogy with cleft constructions in natural 
language.)



More information about the Python-ideas mailing list