Loop until condition is true

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jun 22 05:33:34 CEST 2005

On Wed, 22 Jun 2005 02:01:14 +0200, Remi Villatel wrote:

> Fredrik Lundh wrote:
>>>while True:
>>>	some(code)
>>>	if final_condition is True:
>>>		break
>>>	#
>> checking if a logical expression is true by comparing it to True is bad
>> style, and comparing values using "is" is also bad style.
> Erm... You totally missed the point. I wrote it this way because, first, 
> it's perfectly valid Python code and, second and most important, it's also a 
> valid english sentence.

Why is it more important for Python code to be a valid English sentence
than for it to be valid Python code? 

Does that mean that instead of writing:

squares = [x**2 for x in range(10)]

you instead write the valid English sentence:

the list of squares is given by the set of x squared for x between 0 and 9


The point being, I would have thought that most programmers would have put
"being a valid English sentence" right down at the bottom of their list of
optional conditions, and not as the "most important" requirement ahead of
even being valid code.

I also question your understanding of valid English. I don't know about
you, but I usually say "if the apple is on the bench..." and hardly ever
"if the apple is on the bench is true...".

> [CENSORED] I keep for myself how stupid I found your post.


>> the correct way to write that if-statement is:
> Don't try to teach your grandfather how to suck eggs. 

It must be nice to have learnt everything that there is to learn
about Python. I hope I get there someday.

> There is no "correct 
> way" to code and "superfluous" isn't a synonym of "incorrect".

"if final_condition" will always work.

"if final_condition is True" will break if final_condition is not a bool.
Depending on where final_condition gets its result from, that may or may
not be a high risk. It also relies on an implementation detail, that True
and False are singletons (doubletons?), and will break if that changes.
And finally, since True is just an object, and not a constant like None,
it will break if True gets set to some other value.

In other words, it is not only superfluous but also fragile.


More information about the Python-list mailing list