integer >= 1 == True and integer.0 == False is bad, bad, bad!!!

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Jul 12 06:34:46 EDT 2010


Steven D'Aprano wrote:
>> My complaint (an oddly
>> enough the title of this thread!) concerns the fact that Python treats 0
>> as False and every integer above and below 0 as True. Which is another
>> example of how *some* aspects of Python support bad coding styles.
>>     
>
> Yes, Python does support bad coding styles. The treatment of 0 as a false 
> value is not one of them though.
>
>   

Well, actually some people might think otherwise. While I disagree with 
the OP flaming style, one could argue that muting an integer into a 
boolean makes no sense (I'm one of them). You can still do it, but there 
is no "right" way to do it.
You can decide that everything else than 0 is True, but you could also 
pickup the integer 3 as false value, it would work as well since it 
makes no sense. This is just an arbitrary definition.
Where the OP is fooling himself, is that you can't state that bool-ing 
an integer makes no sense and state the line after that 0 *should* | 
*has to* return True.

I personally can live with 0 == False (with all due respect to our 
C-coding ancestors :) ), however as one of my personal coding rule I 
avoid using it.
I prefere to explicitly write what I want to test:

if myInt <> 0:

or

if myInt is not None:

etc...

That way I do not rely on a  arbitrary int-to-bool mutation.

JM




More information about the Python-list mailing list