Python LogoRecently I was working on some old code. One of my model methods was originally written such that if one parameter was set to the Python None type, a particular set of actions would occur. For some reason, I checked for None like this:

if parameter is None:
    do something

I started running into problems when the changes I was making started passing an empty string into the function rather than None.

My first thought was to cast the empty string to None like this:

if type(parameter).__name__ == "str" and len(parameter) == 0:
    parameter = None

After making the change and doing a bunch of testing, it occurred to me that None and "" are both False in Python. In fact, if I changed my code to just test the parameter itself (if parameter: or if not parameter:), I would be able to handle any type of variable that was passed into the function.

Here's a list of what Python considers to be false.