如何在python中“测试”NoneType?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23086383/
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
How to "test" NoneType in python?
提问by CrveniZg
I have a method that sometimes returns a NoneType value. So how can I question a variable that is a NoneType? I need to use ifmethod, for example
我有一个有时会返回 NoneType 值的方法。那么我如何质疑一个 NoneType 的变量呢?例如,我需要使用if方法
if not new:
new = '#'
I know that is the wrong way and I hope you understand what I meant.
我知道那是错误的方式,我希望你明白我的意思。
采纳答案by thefourtheye
So how can I question a variable that is a NoneType?
那么我如何质疑一个 NoneType 的变量呢?
Use is
operator, like this
使用is
运算符,像这样
if variable is None:
Why this works?
为什么这有效?
Since None
is the sole singleton object of NoneType
in Python, we can use is
operator to check if a variable has None
in it or not.
由于None
是NoneType
Python 中唯一的单例对象,我们可以使用is
运算符来检查变量是否None
存在。
Quoting from is
docs,
引用is
文档,
The operators
is
andis not
test for object identity:x is y
is true if and only ifx
andy
are the same object.x is not y
yields the inverse truth value.
对象标识的运算符
is
和is not
测试:x is y
当且仅当x
和y
是同一个对象时为真。x is not y
产生逆真值。
Since there can be only one instance of None
, is
would be the preferred way to check None
.
由于只能有一个 实例None
,因此is
将是首选的检查方式None
。
Hear it from the horse's mouth
从马口中听到
Quoting Python's Coding Style Guidelines - PEP-008(jointly defined by Guido himself),
引用Python 的编码风格指南 - PEP-008(由 Guido 本人共同定义),
Comparisons to singletons like
None
should always be done withis
oris not
, never the equality operators.
与单例的比较
None
应该总是用is
or来完成is not
,而不是等号运算符。
回答by elhoucine
if variable is None:
...
if variable is not None:
...
回答by cardamom
It can also be done with isinstance
as per Alex Hall's answer:
也可以isinstance
按照Alex Hall 的回答来完成:
>>> NoneType = type(None)
>>> x = None
>>> type(x) == NoneType
True
>>> isinstance(x, NoneType)
True
isinstance
is also intuitive but there is the complication that it requires the line
isinstance
也很直观,但复杂的是它需要线
NoneType = type(None)
NoneType = type(None)
which isn't needed for types like int
and float
.
像int
and这样的类型不需要float
。
回答by Martin Müsli
As pointed out by Aaron Hall's comment:
正如 Aaron Hall 的评论所指出的:
Since you can't subclass
NoneType
and sinceNone
is a singleton,isinstance
should not be used to detectNone
- instead you should do as the accepted answer says, and useis None
oris not None
.
由于您不能子类化
NoneType
并且None
是单例,isinstance
因此不应用于检测None
- 相反,您应该按照公认的答案进行操作,并使用is None
oris not None
。
Original Answer:
原答案:
The simplest way however, without the extra line in addition to cardamom's answeris probably:isinstance(x, type(None))
然而,最简单的方法,除了豆蔻的答案之外没有额外的行可能是:isinstance(x, type(None))
So how can I question a variable that is a NoneType? I need to use if method
那么我如何质疑一个 NoneType 的变量呢?我需要使用 if 方法
Using isinstance()
does not require an is
within the if
-statement:
Usingisinstance()
不需要is
在if
-statement 中:
if isinstance(x, type(None)):
#do stuff
Additional information
You can also check for multiple types in one isinstance()
statement as mentioned in the documentation. Just write the types as a tuple.
附加信息
您还可以检查文档中isinstance()
提到的一个语句中的多种类型。只需将类型写为元组。
isinstance(x, (type(None), bytes))
回答by Pesko
Python 2.7 :
蟒蛇2.7:
x = None
isinstance(x, type(None))
or
或者
isinstance(None, type(None))
==> True
==> 真
回答by Joshua Pachner
Not sure if this answers the question. But I know this took me a while to figure out. I was looping through a website and all of sudden the name of the authors weren't there anymore. So needed a check statement.
不确定这是否回答了问题。但我知道这花了我一段时间才弄明白。我正在浏览一个网站,突然之间作者的名字不再存在了。所以需要一个检查语句。
if type(author) == type(None):
my if body
else:
my else body
Author can be any variable in this case, and None
can be any type that you are checking for.
在这种情况下,作者可以是任何变量,也None
可以是您要检查的任何类型。
回答by alexmosk25
I hope this example will be helpful for you)
我希望这个例子对你有帮助)
print(type(None) # NoneType
So, you can check type of the variable name
因此,您可以检查变量名称的类型
#Example
name = 12 # name = None
if type(name) != type(None):
print(name)
else:
print("Can't find name")