Coding style

Bruno Desthuilliers onurb at xiludom.gro
Thu Jul 20 05:34:12 EDT 2006


Lawrence D'Oliveiro wrote:
> In message <44bcaa47$0$18293$636a55ce at news.free.fr>, Bruno Desthuilliers
> wrote:
> 
> 
>>Lawrence D'Oliveiro wrote:
>>
>>>In message <Q8OdnfqZn6udnSHZnZ2dnUVZ_vOdnZ2d at nmt.edu>, Bob Greschke
>>>wrote:
>>>
>>>
>>>
>>>>I'd go even one step further.  Turn it into English (or your favorite
>>>>non-computer language):
>>>>
>>>>1. While list, pop.
>>>>
>>>>2. While the length of the list is greater than 0, pop.
>>>>
>>>>Which one makes more sense?  Guess which one I like.  CPU cycles be
>>>>damned.
>>>>:)
>>>
>>>
>>>One of my rules is, always program like the language actually has a
>>>Boolean type, even if it doesn't.
>>
>>Python has a boolean type.
> 
> 
> A _proper_ boolean type would _have_ to be used in conditionals.

Try using something that cannot be fed to bool() into a conditional.

> 
>>>That means, never assume that arbitrary values
>>>can be interpreted as true or false,
>>
>>There's nothing to assume, and nothing arbitrary in it.
>> It's all clearly
>>defined in whole letters in the language references.
> 
> 
> Not simply enough.

Then it's a documentation problem.

> 
>>>always put in an explicit comparison
>>>if necessary so it's obvious the expression is a Boolean.
>>
>>The fact that the expression is used in the context of a if statement is
>>clearly enough to denote a boolean expression.
> 
> 
> Which is an inconsistent use of the term "boolean" compared to your
> statement above that "Python has a boolean type", is it not?

No. It is not. A boolean expression is an expression you can pass to
bool().

> 
>>Explicitly testing against a boolean is uselessly redundant...
> 
> 
> Not sure this has anything with what I was saying.

You said:
"""
>>>always put in an explicit comparison
>>>if necessary so it's obvious the expression is a Boolean.
"""

Since the expression following an if will be handled as if passed to
bool() [1], adding a comparison to True will yield a strictly equivalent
result (True == True => True, False == True => False). From a boolean
logic POV, it's a no-op [2]. So it's useless and redundant.


[1] FWIW, it may be just how it works - I've not checked implementation.
Anyway you can easily verify by yourself that it works the same way.

[2] but it still requires extra work from the interpreter - without
changing anything to the result of the test.

FWIW, what would you rather use:

if <some_exp> == True:
  return True
elif <some_exp> == False:
  return False
else:
  raise "???"

or:

return bool(<some_exp>)


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list