Hi, I'm Harlin and welcome to my blog. I write about Python, Alfresco and other cheesy comestibles.

Understanding Pythonic Truthiness and Noneness

I decided recently to pay for a class from Talk Python. I did this to better my skills in Python and to evaluate Michael Kennedy's Python courses. The subjects for his courses seem really interesting and if they pan out, I feel these could be very useful in getting to see how other Pythonistas code. I also promise to give a good evaluation when I'm done with the course I'm taking now.

I consider myself about intermediate when it comes to my knowledge and experience with Python. I have worked on a number of Python and Django projects but feel like I have much more to learn.

If you're mostly self-taught like I am, one of the areas you find yourself is in the "am I doing this right or not?" zone. I think what's crucial to help with this is to associate with other Python coders whenever possible. The #python and #django channels on freenode.net can help a little bit with that. Python and Django meetups are great for that as well. Another thing that helps is investing your time in learning your programming languages set of idioms.

Something that can help with idioms and writing more Pythonic code is using Pylint. In another post, I mentioned using PyLint to do ananlysis on your code. It is a good way to make sure that the code you're writing will be readable by others who are versed in the same discipline as yourself.

The course I'm taking at the moment is called "Write Pythonic Code Like a Seasoned Developer". The course goes over some PEP 8 guidelines as far as how you write the code. One of the things I want to relay from the course is writing Pythonic code to evaluate "truthiness."

Here are some guidelines about what evaluates to False:

  • False is False
  • [] Empty lists/arrays are false
  • {} Empty dictionaries are false
  • "" Empty strings are false
  • 0 An int with the value of zero is false
  • 0.0 A float with a value of 0.0 is false
  • None None type is false
  • And ... everything else is true

So, anytime you're using "if ...", the condition must always evaluate to True if you want the statements in if block to execute. For example:

mynum = 5

if mynum:
    print('mynum has a value of any number that is not zero.')

If we want to evaluate as False, we need to do something like this:

if not mynum:
    print('mynum has a value of zero.')

So, we can then do:

my_list = []

if not my_list:
    print('my_list is empty.')

my_dict = {}

if not my_dict:
    print('my_dict is empty.')

something_to_say = ""

if not something_to_say:
    print('I have nothing to add.')

my_flt = 0.0

if not my_flt:
    print('my_flt must have a value of 0.0')

At the very least, if you're ever in doubt about a variable's truthiness, you can always make use of the bool() function.

my_num = 0
bool(my_num)

=> False

my_num = 100
bool(my_num)
=> True

We also don't want to confuse False with None. Consider this:

my_num = 0
if my_num is None:
...     print('my_num is None.')
... 

my_list = []
if my_list is None:
...     print('my_list is None.')
... 
my_num = None

if my_num is None:
...     print('my_num is None.')
... 
my_num is None.

my_list = None
if my_list is None:
...     print('my_list is None.')
... 
my_list is None.

Just be aware that also, None and False are not the same thing.

Well, stay tuned. I expect my next post to have to do with the Pythonic handling of "or" evaluations in if statements.

Any Comments, Always Welcome!