An Iterator Idiom

Magnus L. Hetland mlh at idt.ntnu.no
Tue May 4 06:47:41 EDT 1999


Moshe Zadka <moshez at math.huji.ac.il> writes:

> Hi!
> 
> I've seen the ``How do you do while(<>) in Python? We need assignment
> in conditionals...No we don't...yes we do....while 1 is evil....is
> not...'' thread once more, I decided to do something about it.
> 
[...]
> 
> How do I use it?
> 
> Well, something like
> 
> for line in iterator(sys.stdin.readline):
> 	sys.stdout.write(line)
> 
> as well as
> 
> for buff in iterator(sys.stdin.read, 1024):
> 	sys.stdout.write(buff)

Nice -- but it has been done before, by several people. (Myself
included, if I am not mistaken). Check out dejanews. I think I called
it repeat. So:

  for line in repeat(sys.stdin.readline):
      process(line)

or something is what you should look for.

(I think it was even suggested as a built-in function.)

> 
> Should work.

--

  Magnus
  Lie
  Hetland        http://arcadia.laiv.org <arcadia at laiv.org>




More information about the Python-list mailing list