[Edu-sig] Truth values and comparisons

Arthur ajsiegel at optonline.net
Mon Oct 30 19:15:50 CET 2006

John Zelle wrote:

>This is why in teaching I prefer to use explicit tests:
>if x != 0:
>    do something
>Rather than 
>if x:
>    do something
Yeah, so in the case I am looking at there is a branching based on 
whether either of 2 arrays are all zeros. 

So to achieve numpy compatibility, and to make the code more 
understandable to folks (like myself) who have some trouble in switching 
gears between numbers being used as numbers and at the same time being 
used for their truth values (kind of a trick really), I might do

if sum(x) !=0:
   use this array
   use other array.

kind of thing.

The case here being distinguishable, in my mind, from the situation 
where one purposefully fills an array with 0's and 1's, with the 
intention of representing truth values. In *that* case the

if sum(x) !=0

would work just as well, but now would seem to be the trick solution 
rather than the elegant one.

Interesting how context matters in these kind of styling questions.

Assuming I am making sense...

Is concerning myself with this distinction sound programming, or is the 
hard core answer more to the effect what works works, what doesn't 
doesn't - and one should focus only on that, and perhaps the performance 
impact of available alternatives?

Though I guess we are all allowed to define "sound programming" for 




More information about the Edu-sig mailing list