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.
Written by Frederik Creemers
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#