在python中确定变量的类型是NoneType
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40553285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determining a variable's type is NoneType in python
提问by splinter
I would like to check if a variable is of the NoneType
type. For other types we can do stuff like:
我想检查变量是否属于该NoneType
类型。对于其他类型,我们可以执行以下操作:
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? Thank you.
但是对于NoneType
这种简单的方法是不可能的。也就是说,我们不能说type(None)==NoneType
。有没有替代方法?为什么这适用于某些类型而不适用于其他类型?谢谢你。
回答by Alex Hall
NoneType
just happens to not automatically be in the global scope. This isn't really a problem.
NoneType
只是碰巧不会自动在全局范围内。这真的不是问题。
>>> 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 test x is None
.
在任何情况下,进行类型检查都是不寻常的。相反,您应该测试x is None
.
回答by Jean-Fran?ois Fabre
Of course you can do it.
你当然可以做到。
type(None)==None.__class__
True