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

Ian Kelly ian.g.kelly at gmail.com
Mon Jul 12 21:28:28 EDT 2010


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.  What he is objecting to is the practice of testing for
special cases of ints (i.e. 0) by treating them as bools.  The
specific details of converting ints to bools are very much relevant
here; as long as 0 is false, it works.  If -1 is false (a semantic I
have actually seen used), then it does not.

Cheers,
Ian



More information about the Python-list mailing list