Python checking for None/Null values

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Aug 11 07:08:46 EDT 2006


Fuzzydave:

> I am trying to check all of the historyRep items
> to check if they are empty/null/None (whatever the term is in python)

An item can't be empty in Python,and null doesn't exist, it can be the
object None. But probly that's not your case.


> I did print
> historyRep[8] out and it falls over, I am assuming if its an array and
> if the SQL query only returns 8 records instead of 10 then the last
> two array values i am checking for litterly don't exist instead of
> being null but i can't find a if exists style function either?

A way to solve your problem is to see how many elements the list
contains with
len(sequence)
then act accordingly with the elements that exist.
Even better is to work on the elements that exist, with something like:
for element in sequence:
    do_stuff

Note: sometimes having a clean and readable program is better than
having a running program that you can't read, because you can fix the
the first one, and it can teach you something.

Bye,
bearophile




More information about the Python-list mailing list