No "side effect" assignment!

Peter Hansen peter at engcorp.com
Mon Sep 15 09:20:42 EDT 2003


Asun Friere wrote:
> 
> Dave Kuhlman wrote:
> > while 1:
> >     foo = getmore()
> >     if not foo:
> >         break
> >     process(foo)
> 
> I am alone in feeling some slight unease at the sight of "while 1: ...
> break"?  I know this is considered somewhat pythonic, but apart from
> the fact that there is only a single assignment statement is there
> really any advantage to be gained by doing it this way?  It seems to
> be trading one aesthetic disadvantage for another, and imho, a
> marginally less legible one.

There is a distinct advantage in the above, over the form with
a duplicated line, and it's nothing to do with aesthetics.  Sure,
they both may be ugly to you (but they feel quite simple and thus
elegant to some experienced Python programmers), but the real issue
is that the one has the duplicated assignment and is therefore prone
to maintenance problems.  This fact alone far outweighs any aesthetic
consideration, and wholly justifies the above form.  

If you are really offended, use a wrapper of some kind as others have
shown.

-Peter




More information about the Python-list mailing list