Best idiom for looping over input?

Jean-Paul Calderone exarkun at divmod.com
Tue Aug 26 18:53:34 CEST 2008


On Tue, 26 Aug 2008 16:42:30 GMT, mh at pixar.com wrote:
>What's the best Python idiom for this C construct?
>
>    while ((x = next()) != END) {
>        ....
>    }
>
>Now I'm doing
>
>    x = next()
>    while x != END:
>        ....
>        x = next()
>
>There's not an iterator for this function, or I would
>just use
>
>    for x in ...
>

Use the magical second parameter to the `iter´ builtin:

    for x in iter(next, END):
        ...

Jean-Paul



More information about the Python-list mailing list