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?
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.
The TypeError: 'NoneType' object is not iterable error is raised when you try to iterate over an object whose value is equal to None. To solve this error, make sure that any values that you try to iterate over have been assigned an iterable object, like a string or a list.
Use the boolean OR operator to convert NoneType to an integer in Python, e.g. result = None or 0 . The boolean OR operator will return the value to the right-hand side because the value to the left ( None ) is falsy. Copied! We used the boolean or operator to return an integer if the value to the left is falsy.
Definition and Usage The None keyword is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string.
Well, in Python 2, 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.
In Python 3, the types.NoneType
name is gone. 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 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