Variable type definition - NoneType in python

I would like to check if the variable is of type NoneType . For other types, we can do things like:

  type([])==list 

But for NoneType this simple way is not possible. That is, we cannot say type(None)==NoneType . Is there an alternative way? And why is this possible for some types and not for others? Thanks.

+6
source share
2 answers

NoneType just doesn't happen automatically in the global area. It's not a problem.

 >>> NoneType = type(None) >>> x = None >>> type(x) == NoneType True >>> isinstance(x, NoneType) True 

In any case, it would be unusual to do a type check. Rather, you should check x is None .

+10
source

Of course you can do it.

 type(None)==None.__class__ True 
+6
source

Source: https://habr.com/ru/post/1012251/


All Articles