Zen of Python

Paul Rubin http
Sat Jan 22 12:09:38 EST 2005

"Andrew Koenig" <ark at acm.org> writes:
> In this case, I think the right solution to the problem is two-fold:
>     1) from __future__ import lexical_comprehensions
>     2) If you don't import the feature, and you write a program that depends 
>     on a list-comprehension variable remaining in scope, the compiler
>     should issue a diagnostic along the lines of
>         Warning: This program should be taken out and shot.

It's not obvious to me how the compiler can tell.  Consider:

    x = 3
    if frob():
       frobbed = True
       squares = [x*x for x in range(9)]
    if blob():
       z = x

Should the compiler issue a warning saying the program should be taken
out and shot?  With lexical comprehensions, the program is perfectly
valid and sets z to 3 if blob() is true.  The whole point of lexical
comprhensions is to make Python safe for such programs.

Without lexical comprehensions, the program still doesn't depend on
the listcomp leakage if frob() and blob() aren't simultaneously true
(envision "assert not frobbed" before the "z = x").  So "should be
taken out and shot" is maybe a little bit extreme.

More information about the Python-list mailing list