SuperBiasedMan
SuperBiasedMan

Reputation: 9969

Why doesn't len(None) return 0?

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?

Upvotes: 27

Views: 38374

Answers (3)

Charles Wilson
Charles Wilson

Reputation: 89

If you have an item that might return None you can use len(None or '') and it will return 0 or the length of the first item if it is not None.

Upvotes: 8

jonrsharpe
jonrsharpe

Reputation: 122024

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.

Upvotes: 22

orlp
orlp

Reputation: 117681

len only makes sense for collections of objects - None is not a collection.

Upvotes: 17

Related Questions