Sunday, December 9, 2012

Truthiness/Falsiness

A student in Udacity cs101, a seeker of truth, posted a question to the forum asking what is actually true and what is false in Python. To read the original question, see: http://forums.udacity.com/questions/2005120/implicit-boolean-evaluation-aka-truthinessfalsiness#cs101

Here is a reposting of my reply to that question:

Your last example:

c = []
random_operation(c) #we do not know if this operation 
                    #  actually adds to list 'c'
if c:               #we only want to execute the following 
                    #  if list 'c' is not empty
    return c[2]     #because if it is empty this will return 
                    #  an IndexError: out of range
troubles me. The if you show doesn't really assure that return c[2] will avoid raising an IndexError. A better guard test would be to ask if len(c) > 2 so you can be sure that c[2] isn't past the end of c.

Somehow your contemplation of what is true, reminded me of this old Gahan Wilson cartoon: Is Nothing Sacred? though if your goal is mastery of Python and not so much mastery of philosophy, there may be more to gain in contemplating this stackoverflow question and its diverse answers: Python elegant assignment based on true false values.

I was surprised at how much digging it took to find a definitive specification of exactly what is true and what is false in Python. The answer I found is here: Python truth values.

By providing the interactive Python interpreter via the course's browser window Udacity avoids saying much about versions of the Python language, perhaps avoids too much - but you should be aware that while there is much discussion of Python 2 vs. Python 3 on the web, that the big deal there is that there were changes made to the language in Python 3 that would break old code (that is, the new version was not upward compatible with the old), but within the sequence of Python 2.x releases, there have also been interesting evolutionary changes to the language.

In looking into things for your question tonight, one surprise was how recently True and False were added to the Python language: PEP 285 - A boolean type, new in Python 2.3. And the language continues to evolve. e.g. PEP 308 - Conditional expressions , new in Python 2.5. But I've never seen a statement of this form in actual use:

x = true_value if condition else false_value
except tonight I gave it a try:
towels=3
print 42 if towels > 0 else None
It actually seems more reminiscent of Perl than Python, but if you are running a 2.5 or newer version of Python, apparently you are entitled to write code in that form. If you have a towel, the answer is 42, but if towels is 0, the answer is None...

Which brought me to wondering exactly what version of Python is in use in Udacity's CS101. Adding this code to a homework problem:

import sys
print sys.version
got me the answer to that question:

2.6.6 (r266:84292, Sep 12 2011, 14:03:02) 
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)]
So, it's a pretty recent version (2.6.6, from August 2010) of Python 2, but it is not 2.7 (not 2.7.3, nor 2.6.8 either, both of which were released in April 2012, if you want to look closer still). If you're curious to know what is the difference between 2.6 and 2.7, see: What's new in Python 2.7. Release 2.6.8 is a maintenance release to provide some security fixes. The history of the dictionary hashing problem is interesting reading, and shows how subtle a flaw can be a security hole these days.