On Tue, 2005-12-06 at 19:58 -0500, Raymond Hettinger wrote:
> Any real-world use cases or compelling contrived examples?
> 
> ISTM, that the code calling it.stop() would already be in position to
> break-out of the iteration directly or set a termination flag.  Instead
> of:
> 
>     it = itertools.interruptable(iterable):
>     for x in it:
>         . . .
>         if cond(x):
>             it.stop()
> 
> Why not write:
> 
>     for x in iterable:
>         . . .
>         if cond(x):
>             break
> 
> If needed, the for-loop can have an else-clause for any processing
> needed in the event of interruption.

The idea was motivated by a case of nested loops, similar to:

    for x in iterable1:
        for y in iterable2:
            for z in iterable3:
                . . .
                if cond1(x):
                    iterable1.stop()
                if cond2(y):
                    iterable2.stop()
                if cond3(z):
                    iterable3.stop()
                . . .

It seemed more convenient at the time than having to deal with multiple
termination flags, or breaks, or a combination thereof.

The ability to remotely terminate a for-loop also struck me as somewhat
interesting:

    def estimate(item, iterable):
        . . .
        if good_enough:
            iterable.stop()
            return result

    for x in iterable:
        . . .
        approx *= estimate(x, iterable)

But these are highly contrived and hardly compelling.

I was primarily interested in whether anyone recalls discussing the
ability to prematurely terminate an iterator and whether there are any
technical drawbacks other than it being redundant.

Matthew Barnes
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to