NULL in Python and Perl

I’m learning a little Python for a side project. I wrote a SELECT statement that would produce NULLs and rather than use COALESCE to prevent returning NULLs, I wanted the application to be able to detect and then replace the value with a default.

In Perl, this is a no-brainer to me – the NULL value would be ‘undef’, and I could write something like:

$value = $default if (! defined $value);

So, after a bit of struggling, I found out that the ‘None’ object is returned when a value is NULL in python.

To determine whether you’ve got a NULL, you write something like:

if $value is None:
    $value = $default

Generally speaking, Python considers an undefined object to be a rare occurrence.

You must be logged in to post a comment.