Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't len(None) return 0?

Tags:

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?

like image 225
SuperBiasedMan Avatar asked May 15 '15 10:05

SuperBiasedMan


People also ask

Does the LEN function count from 0?

Using len() With Built-in Sequences All three data types are valid arguments for len() . In the examples above, you find the length of an empty string, an empty list, and an empty tuple. The function returns 0 in each case.

Does Len start with 0 Python?

The length of the empty string is 0. The len() function in Python is omnipresent - it's used to retrieve the length of every data type, with string just a first example.

What is Len of none in Python?

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 .

What does Len () return in Python?

Definition and Usage The len() function returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string.


2 Answers

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.

like image 96
jonrsharpe Avatar answered Sep 30 '22 15:09

jonrsharpe


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

like image 27
orlp Avatar answered Sep 30 '22 15:09

orlp