Best idiom for looping over input?

M

mh

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

Many TIA!
Mark
 
F

Fredrik Lundh

What's the best Python idiom for this C construct?

while ((x = next()) != END) {
....
}

iter is your friend:

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

details:
Help on built-in function iter in module __builtin__:

iter(...)
iter(collection) -> iterator
iter(callable, sentinel) -> iterator

Get an iterator from an object. In the first form, the
argument must supply its own iterator, or be a sequence.
In the second form, the callable is called until it
returns the sentinel.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top