Checking whether type is None

Iwo Herka hi at iwoherka.eu
Tue Jul 24 15:41:11 EDT 2018


In Python 2, you can import NoneType from types module.
In Python 3, the best you can do is:

    NoneType = type(None)


​Iwo Herka
https://github.com/IwoHerka​

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On 24 July 2018 7:33 PM, Tobiah <toby at tobiah.org> wrote:

> ​​
> 
> Consider:
> 
> >>> type({}) is dict
> 
>     True
>     >>> type(3) is int
>     
>     True
>     >>> type(None) is None
>     
>     False
>     
> 
> Obvious I guess, since the type object is not None.
> 
> So what would I compare type(None) to?
> 
> >>> type(None)
> 
>     <type 'NoneType'>
>     
>     >>> type(None) is NoneType
>     
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     
>     NameError: name 'NoneType' is not defined
>     
> 
> I know I ask whether:
> 
> >>> thing is None
> 
> but I wanted a generic test.
> 
> I'm trying to get away from things like:
> 
> >>> type(thing) is type(None)
> 
> because of something I read somewhere preferring
> 
> my original test method.
> 
> Thanks
> 
> 
> --------------------------------------------------------------------------------------
> 
> https://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list