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?
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.
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.
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 .
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.
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;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; andrange(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.
len
only makes sense for collections of objects - None is not a collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With