It's funny how the little, basic things that you re-examine when you are trying to teach someone else a programming language can surprise you. I thought I knew how these different languages handled this topic but I was wrong. Here we go:
I want to show you some differences between Perl, Python and Ruby (the three of which I consider to be the main "scripting" languages at the moment) with respect to their handling of booleans and boolean expressions. I was taken by surprise with some of this behaviour, as I tend to mainly use booleans as the results of conditional expressions - and fairly explicitly when I do.
No boolean type.
$ perl -e 'if (100 > 1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (100) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (0) { print "true\n" } else { print "false\n" }'
false
$ perl -e 'if (-1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if ("") { print "true\n" } else { print "false\n" }'
false
$ perl -e 'if ("hello") { print "true\n" } else { print "false\n" }'
true
Has a specific boolean type.
>>> x = False
$ python -c 'print ("true" if 100 > 1 else "false")'
true
$ python -c 'print ("true" if 100 else "false")'
true
$ python -c 'print ("true" if 1 else "false")'
true
$ python -c 'print ("true" if 0 else "false")'
false
$ python -c 'print ("true" if -1 else "false")'
true
$ python -c 'print ("true" if "" else "false")'
false
$ python -c 'print ("true" if "hello" else "false")'
true
... ok so it's the same as Perl at this point, but note these following boolean comparisons and compare to the upcoming Ruby code:
>>> 0 == False
True
>>> 1 == True
True
>>> 100 == True
False
>>> -1 == True
False
$ ruby -e 'if (100 > 1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (100) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (0) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (-1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'a = ""; if (a) then puts "true"; else puts "false"; end'
true
$ ruby -e 'a = "hello"; if (a) then puts "true"; else puts "false"; end'
true
... ok ... that's fairly different. That's because Ruby only counts something as false if it is specifically equal to 'false' or 'nil'. Interesting. What about boolean comparisons that aren't wrapped in conditional expressions?
(Python uses 'True' and 'False', Ruby uses 'true' and 'false')
irb(main):001:0> 0 == false
=> false
irb(main):002:0> 1 == true
=> false
irb(main):003:0> 100 == true
=> false
irb(main):004:0> -1 == true
=> false
That is considerably different to Python, right?
What do we know?
OK so this post wasn't exactly straightforward, but it shows how major languages can treat something as simple as booleans so differently.
Django Python 960.gs Git Vim NetBSD Nginx
This is the blog of Brad Willis, a software engineer living in Brisbane.
Help
Latest entries
*BSD Agile Apache Apple apt Athletics Best-Practice Censorship Comedy Cool Crosswords Deployment Django English Exim Firefox Git Hardcore Health irssi Javascript Jira Languages Linux Makefile Mathematics Mobile Broadband Mutt MySQL NetBSD nginx Nokia OpenVZ OSX Perl Privacy Python Rant Requirements rsync Ruby Shell Slackware SQL SQLite SSH Standards Subversion Television Testing ThisBlog Vim VMWare (Fusion) VPN X zsh
Checking for exceptions in doctests
Homer's Curling Speech
retry in Python
Vim Makefile tabs
Centos (or RH) IPTables
Converting ssh2 public keys to openssh
Vim comment hints
Context managers in Perl
Dish rotation
Git - fixing commit user
apt stuff
Using shell variables in AWK
Linux - Too many open files
Tell gvim to save and quit... remotely
Vim - automatically remove whitespace at EOL
Python - relative paths from within modules
TV Aspect Ratios
Git - Which commits are in your branch only?
Subversion setup cheat sheet
Force detach a screen session
Modify sudo's use of environment variables
Install all Perl modules
Mutt - delete old messages
OpenVZ VPS and swap space
fail2ban on NetBSD for ssh
NetBSD - Using sup
Python - testing for a sys.exit
Python Best Practice Link Dump
Python script names
Perl - Using an expensive module
Speed of git clone
Perl Modules with Custom Prefix
Perl: tr vs. s
Brilliant sysadmin Reference
Why is GRUB better than LILO?
Why is swap space important?
Perldoc Output
Git's Index
Jira Project Keys
Git GUI