31

For example looking at the type of None, we can see that it has NoneType:

>>> type(None)
NoneType

However a NameError results when trying to access the NoneType:

>>> NoneType
NameError: name 'NoneType' is not defined

How can the NoneType be accessed?

16
  • 1
    why do you need it ?
    – furas
    Commented Jan 30, 2017 at 4:08
  • 6
    Just do type(what_ever)==type(None) for this one instance.
    – dawg
    Commented Jan 30, 2017 at 4:16
  • 3
    Or, you can do type(what_ever).__name__=='NoneType'
    – dawg
    Commented Jan 30, 2017 at 4:18
  • 1
    Why do you have a validation method for types to begin with? Duck typing, my friend.. And there is already a validation method in isinstance()...
    – kingstante
    Commented Jan 30, 2017 at 4:31
  • 1
    I am wondering why we use equality test == rather than use type(what_ever) is type(None)
    – Peipei
    Commented Jul 17, 2020 at 5:41

2 Answers 2

40

Well, in Python 2, or 3.10+, you can import it from the types module:

from types import NoneType

but it's not actually implemented there or anything. types.py just does NoneType = type(None). You might as well just use type(None) directly.

For some reason, they decided to take the types.NoneType name out in Python 3, before putting it back in 3.10. On versions where the name doesn't exist, just use type(None).


If type(None) shows NoneType for you, rather than something like <class 'NoneType'>, you're probably on some nonstandard interpreter setup, such as IPython. It'd usually show up as something like <class 'NoneType'>, making it clearer that you can't just type NoneType and get the type.


If the reason you want this is for type annotations, use None, not types.NoneType:

def returns_none() -> None:
    return None

Static type checkers special-case this use of None.

7
  • Might as well do type(obj) == type(None), rather than the hassle of extra code?
    – hungryWolf
    Commented Jan 30, 2017 at 4:15
  • user2357112, any idea of how to import it in Python3? I get an import error in Python3: ImportError: cannot import name 'NoneType' @user5676973 Mate you're missing the point.
    – Greg
    Commented Jan 30, 2017 at 4:21
  • Ya okay, I am missing the point, can you explain then? my answer seems to be fine.
    – hungryWolf
    Commented Jan 30, 2017 at 4:27
  • @user5676973 The question did not ask how to validate an input, nor did it ask how to evaluate a type of an object. The question was about how to access the type NoneType. You persistently focus on the previous points and ignore the main focus of the question.
    – Greg
    Commented Jan 30, 2017 at 4:30
  • user2357112, NoneType seems to have been removed as a type in python 3: bugs.python.org/issue19438. Seems that the only way to access the NoneType type in Python3 is the type(None) way.
    – Greg
    Commented Jan 30, 2017 at 4:33
12

As suggested by @dawg in the comments, you can do

if (type(some_object).__name__ == "NoneType"):
  # Do some
  pass

You can also do

NoneType = type(None)    
isinstance(some_object, NoneType) 
4
  • 1
    NoneType = type(None) is interesting for compatibility or something, but for branching if type(some_object) is None is better (PEP 8).
    – Kevin
    Commented Oct 11, 2019 at 15:40
  • I came here looking for what to do when I need NoneType as a key in a dict, but even then it seems to be preferred to do type(None) directly.
    – Kevin
    Commented Oct 11, 2019 at 15:42
  • 1
    @Kevin Also if some_object is None, your type(some_object) is None will never return True.
    – sausix
    Commented Dec 3, 2020 at 12:06
  • 1
    @sausix, thanks. I meant: NoneType = type(None) is a nice, reusable way to work with that, but for branching, the recommended code-style is an identity check like if some_object is None (PEP 8).
    – Kevin
    Commented Dec 3, 2020 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.