Why doesn't len(None) return 0?

SuperBiasedMan picture SuperBiasedMan · May 15, 2015 · Viewed 17.2k times · Source

None in Python is an object.

>>> isinstance(None, object)
True

And as such it can employ functions like __str__()

>>> str(None)
'None'

But why doesn't it do the same for __len__()?

>>> len(None)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    len(None)
TypeError: object of type 'NoneType' has no len()

It seems like it would be Pythonic the same way that if list is acceptable even if the variable is None and not just an empty list.

Are there cases that would make the use of len(None) more of a problem?

Answer

jonrsharpe picture jonrsharpe · May 15, 2015

You mention that you want this:

because it comes up often as an error when a function returns None instead of a list

Presumably, you have code like:

list_probably = some_function()
for index in range(len(list_probably)):
    ...

and are getting:

TypeError: object of type 'NoneType' has no len()

Note the following:

  • len is for determining the length of collections (e.g. a list, dict or str - these are Sized objects). It's not for converting arbitrary objects to integers - it also isn't implemented for int or bool, for example;
  • If None is a possibility, you should be explicitly testing if list_probably is not None. Using e.g. if list_probably would treat None and the empty list [] the same, which is probably not the correct behaviour; and
  • There is usually a better way to deal with lists that range(len(...))- e.g. for item in list_probably, using zip, etc.

Implementing len for None would just hide errors where None is being treated, incorrectly, like some other object - per the Zen of Python (import this):

Errors should never pass silently.

Similarly for item in None would fail, but that doesn't mean that implementing None.__iter__ is a good idea! Errors are a good thing - they help you find the problems in your programs quickly.