Search code examples
pythonpython-3.xtypesnonetype

Where is the NoneType located?


In Python 3, I would like to check whether value is either string or None.

One way to do this is

assert type(value) in { str, NoneType }

But where is NoneType located in Python?

Without any import, using NoneType produces NameError: name 'NoneType' is not defined.


Solution

  • types.NoneType is being reintroduced in Python 3.10.

    What’s New In Python 3.10

    Improved Modules

    types

    Reintroduced the types.EllipsisType, types.NoneType and types.NotImplementedType classes, providing a new set of types readily interpretable by type checkers. (Contributed by Bas van Beek in bpo-41810.)

    The discussion about the change was motivated by a need for types.EllipsisType, leading to types.NoneType also being added for consistency.