问题:如何在Python中“测试” NoneType?

我有一个有时返回NoneType值的方法。那么我如何质疑一个无类型的变量呢?例如,我需要使用if方法

if not new:
    new = '#'

我知道这是错误的方式,希望您能理解我的意思。

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 if method, for example

if not new:
    new = '#'

I know that is the wrong way and I hope you understand what I meant.


回答 0

那么我如何质疑一个无类型的变量呢?

is像这样使用运算符

if variable is None:

为什么这样有效?

由于Python中唯一的单例对象,因此我们可以使用isoperator来检查变量None中是否包含变量。

引用is文档

运算符isis not对象身份测试:x is y当且仅当xy是相同对象时,才为true 。x is not y产生反真值。

由于只能有一个实例Noneis因此是检查的首选方法None


从马口中听到

引用Python的编码样式指南-PEP-008(由Guido亲自定义),

与单例之类的比较None应该总是用isis not不是等式运算符进行

So how can I question a variable that is a NoneType?

Use is operator, like this

if variable is None:

Why this works?

Since is the sole singleton object of in Python, we can use is operator to check if a variable has None in it or not.

Quoting from is docs,

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

Since there can be only one instance of None, is would be the preferred way to check None.


Hear it from the horse’s mouth

Quoting Python’s Coding Style Guidelines – PEP-008 (jointly defined by Guido himself),

Comparisons to singletons like None should always be done with is or is not, never the equality operators.


回答 1

if variable is None:
   ...

if variable is not None:
   ...
if variable is None:
   ...

if variable is not None:
   ...

回答 2

也可以isinstance按照Alex Hall的回答来完成:

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

isinstance 也很直观,但有一个复杂之处,那就是需要

NoneType = type(None)

对于像int和这样的类型,则不需要float

It can also be done with isinstance as per Alex Hall’s answer :

>>> 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

NoneType = type(None)

which isn’t needed for types like int and float.


回答 3

正如亚伦·霍尔的评论所指出的:

由于您不能继承子类,NoneType并且由于None是单例,isinstance因此不应将其用于检测None-而是应按照已接受的答案进行操作,并使用is Noneis not None


原始答案:

但是,最简单的方法,除了豆蔻果实的答案外,没有多余内容可能是:
isinstance(x, type(None))

那么我如何质疑一个无类型的变量呢?我需要使用if方法

使用isinstance()不需要is的内if语句来:

if isinstance(x, type(None)): 
    #do stuff

其他信息
您还可以isinstance()按照文档中的说明在一个语句中检查多种类型。只需将类型写为元组即可。

isinstance(x, (type(None), bytes))

As pointed out by Aaron Hall’s comment:

Since you can’t subclass NoneType and since None is a singleton, isinstance should not be used to detect None – instead you should do as the accepted answer says, and use is None or 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

Using isinstance() does not require an is within the 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(x, (type(None), bytes))

回答 4

不知道这是否能回答问题。但是我知道这花了我一段时间才能弄清楚。我浏览了一个网站,突然间,作者的名字不再存在。因此需要一个检查语句。

if type(author) == type(None):
     my if body
else:
    my else body

在这种情况下,作者可以是任何变量,也None可以是您要检查的任何类型。

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.


回答 5

Python 2.7:

x = None
isinstance(x, type(None))

要么

isinstance(None, type(None))

==>正确

Python 2.7 :

x = None
isinstance(x, type(None))

or

isinstance(None, type(None))

==> True


回答 6

希望这个例子对您有帮助)

print(type(None) # NoneType

因此,您可以检查变量名称的类型

#Example
name = 12 # name = None
if type(name) != type(None):
    print(name)
else:
    print("Can't find name")

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")

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。