No, loop-and-a-half! (Re: REPEAT... UNTIL ?)

Alex Martelli aleax at aleax.it
Fri Jul 5 13:31:36 EDT 2002


Michele Simionato wrote:
        ...
>>    while:
>>      line = f.readline()
>>    gives line <> "":
>>      do_something()
>> 
>> Some day I'll get around to PEPping this...
> 
> Python2.2 has already
> 
> for line in f: do_something()
> 
> where f is a file object.

...and for those cases where the function called is
not specifically f.readline(), iter can still help:

while:
    blup = anobj.amethod()
    if blup == '': break
    do_something(blup)

becomes:

for blup in item(anobj.amethod, ''):
    do_something(blup)


You can take this as an indication of things to come,
maybe -- appropriate iterators or other generators do
let you express just about any loop as a for.

My tutorial about iterators and generators is up for
download at the Europython site, www.europython.org,
since I just presented it at the Europython conference.

Not sure about the URL, and www.europython.org is not
responding right now so I can't check -- anyway, what's
up for sure is a ZIP of the .PPT (Microsoft Powerpoint
file) -- Marc Andre Lemburg also kindly turned it into
a .PDF file, but I don't know if that's up for download
yet (but should be pretty soon anyway).


Alex




More information about the Python-list mailing list