0

I'm looking for guidance on a good convention as to when it is appropriate and/or desirable to return None from a Python function as opposed to an empty list or a zero-length string?

For example, right now, I am writing a Python class to interface to GIT (for the moment, lets not worry about why I have to write this from scratch), and several functions return values. Take for example a function called, "get_tags". Normally, it returns a list, but, if there are no tags yet in the repo, is it better to return an empty list or None?

I realize there will be multiple views on this, which is I guess why I'm asking the question. I'm sure there are pro's and con's to both, which is the information I'm seeking.

Is there a general convention on this?

MACS
  • 163
  • 1
  • 4
  • Already has been discussed [here](http://stackoverflow.com/questions/15300550/python-return-return-none-and-no-return-at-all) –  Nov 24 '14 at 19:48
  • 3
    If your function is supposed to return a list of tags, and the number of tags to return happens to be zero, it should return an empty list. Then whatever calls the function might not have to handle the special case differently. – khelwood Nov 24 '14 at 19:48
  • 2
    It's hard for me to think of a case where `None` has advantages over, say, an empty list. On the other hand, an empty list allows you to write code such as `my_list.extend(get_tags(...))`, since `get_tags` is designed to always return a list. My feeling (and it's not really a convention, per se), is that `None` might be returned if something anomalous occurred in the function call, but you didn't want use exceptions for whatever reason. – jme Nov 24 '14 at 19:50
  • 1
    @kiran.koduru, that's related, but different. This post is asking whether to return None or something else with a False truthiness, such as `[]` or `""`. – Kevin Nov 24 '14 at 19:50
  • 2
    I'll throw in my rule of thumb: if your function usually returns a string, return `""`. if it usually returns a list, return `[]`. Return `None` otherwise. – Kevin Nov 24 '14 at 19:52
  • `None` and `[]` and `""` are all False values in python. [link](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#truth-values) –  Nov 24 '14 at 19:52
  • @kiran.koduru That is not an argument for using `None` in place of a value of the correct type. By that logic, you could also return 0 instead of an empty list. – chepner Nov 24 '14 at 19:54

2 Answers2

1

What is the most common usage for the data returned by the get_tags function? I would assume at some point you're going to want to iterate over it. Consider the following:

Lists, tuples, and dicts

# returning a list
for tag in get_tags():
    print tag

# returning None
tags = get_tags()
if tags is not None:
    for tag in tags:
        print tag

In the above example, returning None is more tedious and less readable than returning an empty list since you have to check that tags is a valid iterable before trying to iterate over it. An empty list is a valid iterable and does not need the check. Dictionaries and tuples are similar.

Strings

For strings, I find myself doing string manipulation or searching on a result that will most often return a string:

import re

description = commit.get_description().lower()
# get all JIRA issues
for project_name, issue_id in re.findall(r'(\w+)-(\d+)', s):
    jira.close_issue(project_name, issue_id)

# vs.

description = commit.get_description()
if description:
    description = description.lower()
    for project_name, issue_id in re.findall(r'(\w+)-(\d+)', s):
        jira.close_issue(project_name, issue_id)

Instances of Classes

It make sense to return None for an instance of a class in cases where there is a test for existence.

# using xml.etree.ElementTree as an example,
# the .find() method of an XML element will return None if
# an Element is not found
my_tag = root.find('my-tag')
if my_tag is not None:
    do_something_with_my_tag(my_tag)

Summary

The return value should implement all of the expected methods for normal usage, i.e. duck typing (except, of course, the last case where None is usually better for instances).

OozeMeister
  • 4,638
  • 1
  • 23
  • 31
0

I'd say that if the function should return an object, then return None. If your function should return a "native" type (string, int, list, dict, etc...) then return "", 0, [], {}, etc...

Matt
  • 1,313
  • 4
  • 17
  • 27