Skip to content
Advertisement

When should I use None vs False?

From what I’ve seen, the only time None and False behave differently is when they’re compared to each other (for equality). Am I missing something, or is it ok to use only one or the other as long as you’re consistent in your usage? Is there any reason to use one over the other?

Advertisement

Answer

I would emphasize here the semantical difference rather than the behavioral.

Let’s consider a couple of functions:

def find_user(criteria):
    if is_met(criteria):
        return User(...)
    return None

# ...vs 

def has_email(user):
    return bool(user.email)

To me, in the above example, None means an absence of the value, while False means the boolean falsity.

An analogy here could be an emergence of the bool type in C++. Historically, C (ab)used int for boolean logic, but C++ introduced a dedicated bool type which makes intentions behind some variables/function signatures clearer.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement