Testing conditions.

Jeremy Bowers jerf at jerf.org
Wed Feb 9 20:01:03 EST 2005


On Thu, 10 Feb 2005 05:38:54 +0000, Ray Gibbon wrote:
> e.g. 2
> |    while new_data, environment = get_more_data(source):
> |        process_data(new_data, environment)
> 
> is something I'm equally likely to want to do, but I can't express it's
> meaning.

I think we'd usually phrase that as 

for new_data, environment in data(source):
	process_data(new_data, environment)

and if data(source) isn't already an iterator of one form or another (a
list, a generator, etc.), you can easily write it to be one; most of the
time it already is.

I use while, but much, much more rarely than for, even for things I'd
write as "while" in other languages, and the majority of the "while"s are
infinite generators, e.g.:

def counter():
  i = 0
  while True:
    yield i
    i += 1




More information about the Python-list mailing list