Thursday, May 29, 2008

Python and Ruby Differences: 0 == ?

So I've been learning Ruby and Ruby On Rails over the last few weeks for a new project. Today I ran across something, well, different:

if 0 then puts "0 == true!" end

Returns "0 == true". A "Gotcha!" moment for this old C hacker.

Now Python:

if 0: print "0 == true" else: print "0 == false"

Returns "0 == false".

Investigating this a bit more:

$ irb >>> 0 == false => false >>> 0 == true => false

$ py Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 0 == True False >>> 0 == False True

More info: Ian Bicking talks about Truth in Python and Ruby some too (and other topics).