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

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Jul 13 07:24:11 EDT 2010


Steven D'Aprano wrote:
> On Mon, 12 Jul 2010 19:28:28 -0600, Ian Kelly wrote:
>
>   
>> On Mon, Jul 12, 2010 at 6:18 PM, Steven D'Aprano
>> <steve at remove-this-cybersource.com.au> wrote:
>>     
>>>> I prefere to explicitly write what I want to test:
>>>>
>>>> if myInt <> 0:
>>>>         
>>> I would argue against that. Why do you, the coder, care about the
>>> specific details of treating ints in a boolean context? The int type
>>> itself knows, leave the decision to it.
>>>       
>> I think you're missing the point.  He's not using ints in a boolean
>> context.  If it were a boolean context, he would be using bools in the
>> first place.
>>     
>
> Of course he is -- he's branching on an int (a boolean context), and 
> explicitly writing out the test as myInt <> 0. In fact, that test could 
> just as easily be written as bool(myInt), which has the benefit of being 
> even more explicit that you want a bool.

I never use integers as boolean value. There is no need to in python, 
since True and False are available. I use integers only as integers 
(hope you see what i mean, i.e to count a number of...).
Knowing that, the only meaning possible would be

if myInt:

equals to

if myInt is not None:

in other words, if myInt is a meaningful integer.

But this is wrong in python, as "if myInt" means "if myInt <>None and 
myInt <>0" (I experienced bug because of my wrong understanding).
Fine I can live with that. It forces me to write explicitly the 
condition I require, which is a good thing (explicit >> implicit).

JM

PS : you made a typo, myInt <> 0 does not equal to bool(myInt) (when 
myInt is None)





More information about the Python-list mailing list