rohit
Comments
Reactions

Fun with none

By : rohit

(If you are in a hurry, here is the fun part.;) A few days ago, I was working with a nullable field, which wasn't behaving as I expected. So I started a shell, and see how nulls compare.

In [1]: None
In [2]: None > 10
Out[2]: False
In [3]: None < 10
Out[3]: True
In [4]: None == None
Out[4]: True
Funny, not as I expected. (Why is None < 10 True. I thought it would be either False or None or cause an Error?) So python is obviously broken, next steps, see the same thing in some other language. Lets try some other language, Javascript maybe?
//#Javascript
>>> null
null
>>> null == 10
false
>>> null > 10
false
>>> null < 10
true
>>> null == null
true
Ruby?
irb(main):001:0> nil
=> nil
irb(main):002:0> nil > 10
NoMethodError: undefined method `>' for nil:NilClass
  from (irb):2
  from :0
irb(main):003:0> 10 > nil
ArgumentError: comparison of Fixnum with nil failed
  from (irb):3:in `>'
  from (irb):3
  from :0
irb(main):004:0> 10 < nil
ArgumentError: comparison of Fixnum with nil failed
  from (irb):4:in ` 10 == nil
=> false
So, this piqued my curiosity and without further ado, here is how None/nil/null/Nothing behave in fifteen different languages. You would assume they would have sorted something this basic out by now. :)

Comparision of how null behaves in different languages for

  • null > 10
  • null < 10
  • null == 10
  • null == null
Null > 10 Null < 10 Null == null 10 == 10 Null == 10 names Link
Python FALSE TRUE TRUE TRUE FALSE None http://gist.github.com/147892
Ruby Error Error TRUE TRUE FALSE nil http://gist.github.com/147894
Lua Error Error TRUE TRUE FALSE nil http://gist.github.com/147906
Javascript FALSE TRUE TRUE TRUE FALSE null http://gist.github.com/147909
Mysql null null null TRUE null null http://gist.github.com/147921
Psql null null null TRUE null null http://gist.github.com/147915
Sqlite null null null TRUE null null http://gist.github.com/147919
Haskell Error Error TRUE TRUE Error Nothing http://gist.github.com/147953
Clojure Error Error Error TRUE Error nil http://gist.github.com/147958
Java Error Error TRUE TRUE Error null http://gist.github.com/147932
Groovy FALSE TRUE TRUE TRUE FALSE null http://gist.github.com/147936
Perl Error Error Error TRUE Error undef http://gist.github.com/147925
Scala Error Error TRUE TRUE FALSE null http://gist.github.com/147923
Jython FALSE TRUE TRUE TRUE FALSE None http://gist.github.com/147914
Php null TRUE TRUE TRUE null null http://gist.github.com/147899
All these are high level languages, and with exception of Java/Haskell/Sql are dynamically typed, so why is there this much difference in how None/null/nil as a type is handled? ---- As you can tell, I am very new to some of these languages, so if I have got your favorite language wrong. Please let me know in the comments and I will fix it.

Related Posts


Can we help you build amazing apps? Contact us today.

© Agiliq, 2009-2012