本文翻译自:How to “test” NoneType in python?
I have a method that sometimes returns a NoneType value. 我有一个有时返回NoneType值的方法。 So how can I question a variable that is a NoneType? 那么我如何质疑一个无类型的变量呢? I need to use if method, for example 例如,我需要使用if方法
if not new:
new = '#'
I know that is the wrong way and I hope you understand what I meant. 我知道这是错误的方式,希望您能理解我的意思。
#1楼
参考:https://stackoom.com/question/1Yrp1/如何在Python中-测试-NoneType
#2楼
So how can I question a variable that is a 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
是Python中NoneType
的唯一单例对象, NoneType
我们可以使用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. 运营商is
和is not
测试对象的身份:x is y
为真,当且仅当x
和y
是同一个对象。x is not y
yields the inverse truth value.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
oris not
, 永远不要用等于运算符 。
#3楼
if variable is None:
...
if variable is not None:
...
#4楼
It can also be done with isinstance
as per Alex Hall's answer : 也可以按照Alex Hall的回答通过isinstance
完成:
>>> 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)
which isn't needed for types like int
and float
. 对于int
和float
这样的类型则不需要。
#5楼
As pointed out by Aaron Hall's comment: 正如亚伦·霍尔的评论所指出的那样:
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
子类NoneType
并且None
是单例,因此不应使用isinstance
来检测None
,而应按照已接受的答案进行操作,并且useis None
或is not None
。
Original Answer: 原始答案:
The simplest way however, without the extra line in addition to cardamom's answer is probably: 但是,最简单的方法,除了豆蔻果实的答案之外,没有多余的内容可能是: isinstance(x, type(None))
So how can I question a variable that is a NoneType? 那么我如何质疑一个无类型的变量呢? I need to use if method 我需要使用if方法
Using isinstance()
does not require an is
within the if
-statement: 使用isinstance()
不需要is
该范围内if
语句来:
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 . 您还可以按照文档中所述在一个isinstance()
语句中检查多种类型。 Just write the types as a tuple. 只需将类型写为元组即可。
isinstance(x, (type(None), bytes))
#6楼
Python 2.7 : Python 2.7:
x = None
isinstance(x, type(None))
or 要么
isinstance(None, type(None))
==> True ==>正确