如何在Python中“测试” NoneType?

本文翻译自: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 and is not test for object identity: x is y is true if and only if x and y are the same object. 运营商isis not测试对象的身份: x is y为真,当且仅当xy是同一个对象。 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 with is or is not , never the equality operators . 与单例的比较(如None应该总是用is or is 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 . 对于intfloat这样的类型则不需要。


#5楼

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 . 由于您不能将NoneType子类NoneType并且None是单例,因此不应使用isinstance来检测None ,而应按照已接受的答案进行操作,并且use is Noneis 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 ==>正确

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值