python 同0(zero)比较的写法

python 同0(zero)比较的写法

author: archimekai

在PEP8中,规定了一些情况的比较写法,比如和True和False比较的写法,判断列表是否为空的写法。但是,PEP8没有明确说明和0比较时,如何书写。

目前来看,判断变量是否为0,有两种写法:

写法一: if x == 0

最直接的写法。而且符合PEP20中的explicit is better than implicit原则。

写法二: if not x

这种写法利用了python中,0.__bool__() 为 False的事实。不过没有上下文时,不容易看出来这样比是想判断x为0.

两种写法对比如下

比较项目写法1 if x == 0写法2 if not x
简洁性简洁
意义明确性(是否能让人一眼看出和0做比较)否(不能一眼看出是在和0比)
特殊场景(x=None)结果为False结果为True
特殊场景(x=‘’)结果为False结果为True
特殊场景(x=[])结果为False结果为True

通过比较可见,if not x的写法更为简洁,但是当x的类型不是预期的类型时,not x的结果可能和x == 0的结果不一致。

关于用那种写法更好,没有结论。

PEP 8中的相关规定

和True或False比较时,不要使用==

Don’t compare boolean values to True or False using ==:

Yes:

if greeting:

No:

if greeting == True:

Worse:

if greeting is True:

注意这里强调的是boolean values,python中的术语boolean values仅指False和True。也就是说,PEP8没有明确规定和0比较的写法。

对于list是否为空的判断,应该使用 if mylist

For sequences, (strings, lists, tuples), use the fact that empty sequences are false:

Yes:

if not seq:
if seq:

No:

if len(seq):
if not len(seq):

python 官方文档中的相关描述

关于bool类

class bool([x])
Return a Boolean value, i.e. one of True or False. x is converted using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. The bool class is a subclass of int (see Numeric Types — int, float, complex). It cannot be subclassed further. Its only instances are False and True (see Boolean Values).

Boolean Values的定义

Boolean Values

Boolean values are the two constant objects False and True. They are used to represent truth values (although other values can also be considered false or true). In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively. The built-in function bool() can be used to convert any value to a Boolean, if the value can be interpreted as a truth value (see section Truth Value Testing above).

They are written as False and True, respectively.

python中对一个对象计算bool值的算法

Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. [1] Here are most of the built-in objects considered false:


•constants defined to be false: None and False.
•zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
•empty sequences and collections: '', (), [], {}, set(), range(0)

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

相关资料

PEP8: https://www.pep8.org

PEP20: https://www.python.org/dev/peps/pep-0020/

tutor上关于和0比较写法的讨论:https://mail.python.org/pipermail/tutor/2009-October/072005.html

stackoverflow上的讨论: https://stackoverflow.com/questions/3216681/which-of-if-x-or-if-x-0-is-preferred-in-python

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值