Last Updated: February 25, 2016
·
464
· bigblind

Explicitly return None in Python

When your function is not just there for its side effects, so it's expected to return something, explicitly return None at the end, for clarity.

For instance, I just wrote the following method::

class Token(object):
    ...

    @classmethod
    def get_user(cls, auth_provider, auth_token, user_id):
        token = cls.get_by_key(cls.make_key(auth_provider, auth_token, user_id))
        if token:
            return token.user.get()

As it is written here, this function works as expected, if the token isn't found, the function returns None, but you have to think about that. When others read your code, you want to reduce the amount of brain cycles needed. Explicitly returning None means one less thing they have to worry about.