Tuesday, 25 September 2012

Python's Null Values

A problem which I and many others have encountered when using python for GIS tasks is the identification of python's different ways of representing an empty value, generally described in other languages as NULL.

For example, in SQL, to select a series of rows from a table where an attribute is NULL is performed using the command:

SELECT * FROM [TABLE_NAME] WHERE [FIELD] IS NULL

This query will return any row in which FIELD is empty, regardless of why it is empty.

In python things are a bit more complex, there are three possible types of null which python can deal with and knowing which null type you are using is important in avoiding seemingly inexplicable errors from cropping up.

The three types are:
  1. None - the python equivalent of SQL's NULL
  2. "" - an empty string
  3. An unassigned variable
These three types can be demonstrated at the python shell:
Clearly an unassigned variable is useless as it generates an error, but the x and y values can both be used, but are they equal?
>>> x == y
False
>>> 
They may conceptually be the same thing, but the interpreter considers them to be distinct, which can present problems when trying to set loop conditions, or run models until a null value is filled or encountered. The key is to be clear at the outset which kind of null values you will be encountering.

No comments:

Post a Comment