PEP: statements in control structures (Re: Conditional Expressions don't solve the problem)
Christopher A. Craig
com-nospam at ccraig.org
Fri Oct 19 09:45:36 EDT 2001
huaiyu at gauss.almadan.ibm.com (Huaiyu Zhu) writes:
> Carl Banks gave some well-thought out objection, which I'd like to summarize
> them as:
>
> (1) There may be many statements before the condition, resulting in long
> while-lines.
> (2) The else-clause for while is not necessary for while 1.
>
> But then I was reminded of the examples I collected by searching for break
> statements in the source distribution. Most usages are similar to
[example with a single assignment followed by a test]
I disagree. I used zsh to do
grep 'while 1' **/*.py
in the standard library and then looked at the loops in the first few
files that came back and I found that
8 of those files had forms like
# asyncore.py line 468
while 1:
tbinfo.append ((
tb.tb_frame.f_code.co_filename,
tb.tb_frame.f_code.co_name,
str(tb.tb_lineno)
))
tb = tb.tb_next
if not tb:
break
or
# code.py line 229
while 1:
try:
if more:
prompt = sys.ps2
else:
prompt = sys.ps1
try:
line = self.raw_input(prompt)
except EOFError:
self.write("\n")
break
else:
more = self.push(line)
except KeyboardInterrupt:
self.write("\nKeyboardInterrupt\n")
self.resetbuffer()
more = 0
which do not translate well to your model, 3 have actual infinite
loops or loops that terminate by some means other than a break
(exception or return), 6 have loops that have a single statement
before the test, and 2 have a combination of these. If this were
statistically significant (which it isn't as I am way too lazy to
write a parser to try a significant sample), it would imply that you
will fix somewhere around half of the instances of 'while 1:'
structures, at the cost of adding a structure to Python that requires
the use of semicolon delimited statements.
I will go ahead and state that I think the current 'while 1: if not'
loops are more readable than those your proposal, and that
iterators/generators solve the vast majority of problems that you have
raised, but I really only wanted to present this to make sure that the
Carl's first point does not get understated in your PEP (which I
believe you should finish, if for no other reason than hopefully
putting this argument to rest).
--
Christopher A. Craig <com-nospam at ccraig.org>
More information about the Python-list
mailing list