How to do this in Python...

Martin Maney maney at pobox.com
Tue Jan 28 15:18:25 EST 2003


Carl Banks <imbosol at vt.edu> wrote:
> I didn't have to guess that's what you would think.

Next time why don't you write down your predicition and save me the
trouble of replying?  <wink>

> I'd like to point out that a similar "abuse" of control structure is
> commonly advocated on this newsgroup, namely the while 1: idiom.

I'm not sure which idiomatic usage you're referring to.  "while 1"
seems to be exactly right when the intent is that the loop's
termination just cannot be stated there; it certainly seems more
sensible than "for i in xrange(positive_infinity)", no?

The "abuse" of while that seems most common is using it in place of a
loop that would be written using C's do...while form, but I really
can't see calling that abuse; it is, once again, merely the least
distasteful way of expressing the natural shape of the computation in a
language that doesn't have a better way of stating it.  Duplicating a
chunk of code by copy and paste is *not* a better way, except perhaps
for that mythical code that will be broken down into raw bits after
it's been used once.  Otherwise duplicated code is a mistake waiting to
happen, if it hasn't already.

Then again, perhaps you're thinking of the similar use where the test
can be written at the top, just not, or not readily, as a Python
expression.  That's an unfortunate usage, but again I can't see calling
it an abuse when it is the best way of expressing etc., etc.

Or is it something else altogether - maybe something I haven't seen so
often as to consider it common?

> I suppose "for i in [1]:" isn't as transparent as "while 1:", but
> idiom it's used for isn't as common.

What does "for i in [1]" get you that "while i" doesn't, aside from a
bit of namespace pollution?

[at this point I was interrupted, which may have contributed to my
seeing things differently when I continued]

Oh.  You like it that it says the loop isn't a real loop.

It still seems strained, but part of me can see that point of view.  I
have to screw my face up and keep one eye almost closed, but...




More information about the Python-list mailing list