The advice seems poor to me. When you're iterating over some kind of collection, it is usually better to use one of Python's iteration tools, but that doesn't mean that while
is always wrong. There are lots of cases where you're not iterating over any kind of collection.
For example:
def gcd(m, n):
"Return the greatest common divisor of m and n."
while n != 0:
m, n = n, m % n
return m
You could change this to:
def gcd(m, n):
"Return the greatest common divisor of m and n."
while True:
if n == 0:
return m
m, n = n, m % n
but is that really an improvement? I think not.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…