Schilcote
Schilcote

Reputation: 2404

Why does None lack a docstring?

As far as I'm aware, every builtin object in Python has a docstring. Except, as I just found out today, None. Why is this?

Upvotes: 1

Views: 244

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124100

None has no other function than to exist. It is one instance, not a type, while other built-ins are invariably callable or are instances of something that is.

Strings, numbers, booleans, etc. all have a callable type object, that often has specific conversion functionality as well (bool(), int(), str()). None doesn't; it's type doesn't produce anything because there is just the one instance:

>>> type(None)
<type 'NoneType'>
>>> type(None)()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create 'NoneType' instances

None is not the only such object. Ellipsis and NotImplemented are other such objects:

>>> Ellipsis.__doc__ is None
True
>>> NotImplemented.__doc__ is None
True
>>> type(Ellipsis)()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create 'ellipsis' instances
>>> type(NotImplemented)()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create 'NotImplementedType' instances

They are all sentinels, objects that signal a specific state or value, but are not useful outside of that function.

As such, they may have meaning in how they are used, not how they are created. The correct place to document this is the Datamodel reference documentation, not in docstrings.

Upvotes: 2

Related Questions