[Python-Dev] Withdrawn PEP 288 and thoughts on PEP 342

Joachim Koenig-Baltes joachim.koenig-baltes at emesgarten.de
Fri Jun 17 22:53:32 CEST 2005


Guido van Rossum wrote:

>
>However, I can see other uses for looping over a sequence using a
>generator and telling the generator something interesting about each
>of the sequence's items, e.g. whether they are green, or should be
>printed, or which dollar value they represent if any (to make up a
>non-Boolean example).
>
>Anyway, "continue EXPR" was born as I was thinking of a way to do this
>kind of thing in Python, since I didn't want to give up return as a
>way of breaking out of a loop (or several!) and returning from a
>function.
>
>But I'm the first to admit that the use case is still very much
>hypothetical -- unlike that for g.next(EXPR) and VAR = yield.
>
>  
>
My use case for this is a directory tree walking generator that
yields all the files including the directories in a depth first manner.
If a directory satisfies a condition (determined by the caller) the
generator shall not descend into it.

Something like:

DONOTDESCEND=1
for path in mywalk("/usr/src"):
    if os.path.isdir(path) and os.path.basename(path) == "CVS":
        continue DONOTDESCEND
    # do something with path

Of course there are different solutions to this problem with callbacks
or filters but i like this one as the most elegant.

Joachim







More information about the Python-Dev mailing list