On Mon, 12 Jul 2010 19:28:28 -0600, Ian Kelly wrote: > On Mon, Jul 12, 2010 at 6:18 PM, Steven D'Aprano > <st...@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. It's also silly. Explicitness is not always a virtue. Beginners sometimes write things like: s = "hello " t = "world" print str(s + t) and we rightly shake our heads at the sheer n00b-ness of it. Writing the explicit tests: if bool(myInt): or even: if myInt <> 0: are firmly in the same category. The only difference is that it is more familiar and therefore comfortable to those who are used to languages that don't have Python's truth-testing rules. > 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. You've seen -1 being used as false? Where? Are you still talking about Python builtins or language semantics? If you're about to say string.find(substring), no, absolutely not. find does not return a true/false flag, it returns an index, with -1 the sentinel for Not Found. This is not a flag! In fact, one of the motivations for adding bools to Python was to avoid people mistakenly thinking that cmp(a, b) returned a flag 0, 1 when it actually returns a three-state -1, 0, 1. This was a common source of errors before Python got bools. -- Steven -- http://mail.python.org/mailman/listinfo/python-list