piiiipes!!

Alex Martelli aleax at aleax.it
Fri Aug 31 12:28:57 CEST 2001


"Ignacio Vazquez-Abrams" <ignacio at openservices.net> wrote in message
news:mailman.999229007.29977.python-list at python.org...
> On 30 Aug 2001, Jeff Grimmett wrote:
>
> >   while (t = file_1_read_object.read()):
> >      process_stuff()
> >      file_2_write_object.write(t)
>
> t = file_1_read_object.read()
> while t:
>   process_stuff()
>   file_2_write_object.write(t)
>   t = file_1_read_object.read()
>
> Granted, it's ugly, but it works.

It has the serious defect of violating the "once, and only
once" precept:-).  I'd rather avoid duplicating the read call:

while 1:
    t = whatever.read()
    if not t: break
    t = process(t)
    blahblah.write(t)

However, see also my "Assign and test" recipe on the Cookbook,
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66061,
if you're really keen on being able to code this as, e.g.:

while data.set(whatever.read()):
    data.set(process(data.get()))
    blahblah.write(data.get())

The 'DataHolder' class to which instance data belongs is just
a 4-liner, after all, and you can place it in your sitecustomize
module if you're *really* fond of this idiom:-).


Alex







More information about the Python-list mailing list