Paul Rubin <http://[EMAIL PROTECTED]> wrote:

> [EMAIL PROTECTED] (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.

Yes, that's how C++ introduced it, for example.  But note that, after
waffling quite a bit in various betas of VC++, Microsoft ended up having
this form *not* limit the scope, for years, in two major releases; I'm
not privy to their reasons for accepting the syntax but rejecting its
key semantic point, and I think they've finally broken with that in the
current VC++ (don't know for sure, haven't used MS products for a
while).  But it sure made for quite a long transition period.

It's far from clear to me that it's worth complicating Python by
introducing a third form of loop, next to normal while and for ones, to
mean "a for loop whose control variables are hyperlocalized" (plural, in
general, of course -- ``for n, v in d.iteritems():'' etc).


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

You can abort them by having an exception raised -- that's quite a
different issue.

>     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"

This way, you don't get anything assigned to lcomp.  break is quite
different from raise, which aborts the whole caboodle up to the handler.


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to