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.
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?In [1]: NoneIn [2]: None > 10Out[2]: FalseIn [3]: None < 10Out[3]: TrueIn [4]: None == NoneOut[4]: True
//#Javascript
>>> null
null
>>> null == 10
false
>>> null > 10
false
>>> null < 10
true
>>> null == null
true
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 => falseSo, 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 |
Related Posts
- __new__() in python
- Introduction to Python Workshop on February 15th, 2013
- Metaclass in Python
- Understanding '*', '*args', '**' and '**kwargs'
- Understanding decorators
Can we help you build amazing apps? Contact us today.
You have the Scala table wrong. You should be using scala.Option. Scala's null is a Java hangover is better to be thought of as similar to Haskell's undefined.