Zen of Python
Paul Rubin
http
Sat Jan 22 07:40:25 EST 2005
aleaxit at yahoo.com (Alex Martelli) writes:
> If it changed the semantics of for-loops in general, that would be quite
> inconvenient to me -- once in a while I do rely on Python's semantics
> (maintaining the loop control variable after a break; I don't recall if
> I ever used the fact that the variable is also maintained upon normal
> termination).
Some languages let you say things like:
for (var x = 0; x < 10; x++)
do_something(x);
and that limits the scope of x to the for loop. That seems like a
reasonable way to offer for-loops that don't leak.
> (musing...): I think the reason there's no real use case for using a
> listcomp's control variable afterwards is connected to this distinction:
> listcomps have no `break'...
Of course you can still break out of listcomps:
class oops: pass
def f(x):
if x*x % 11 == 3: raise oops
return x*x
try:
lcomp = [f(x) for x in range(10)]
except oops: pass
print x
prints "5"
More information about the Python-list
mailing list