简体   繁体   English

在if语句中检查0的Pythonic方法?

[英]Pythonic way of checking for 0 in if statement?

In coding a primality tester, I came across an interesting thought. 在编写素数测试器时,我遇到了一个有趣的想法。 When you want to do something if the result of an operation turns out to be 0 , which is the better ('pythonic') way of tackling it? 当你想要做一些事情,如果一个操作的结果是0 ,这是更好的('pythonic')解决它的方式?

# option A - comparison
if a % b == 0:
    print('a is divisible by b')

# option B - is operator
if a % b is 0:
    print('a is divisible by b')

# option C - boolean not
if not a % b:
    print('a is divisible by b')

PEP 8 says that comparisons to singletons like None should be done with the is operator. PEP 8表示,应该使用is运算符来比较像None这样的单例。 It also says that checking for empty sequences should use not , and not to compare boolean values with == or is . 它还说检查空序列应该使用not ,而不是将布尔值与==is进行比较。 However, it doesn't mention anything about checking for a 0 as a result. 但是,它没有提到任何关于检查0的结果。

So which option should I use? 那么我应该使用哪个选项?

Testing against 0 is (imo) best done by testing against 0 . 针对0测试是(imo)最好通过针对0进行测试来完成。 This also indicates that there might be other values than just 0 and 1 . 这也表明可能存在除01之外的其他值。

If the called function really only returns 0 on success and 1 on fail to say Yes/No , Success/Failure , True/False , etc., then I think the function is the problem and should (if applicable) be fixed to return True and False instead. 如果被调用的函数实际上只在成功时返回01则未能说Yes/NoSuccess/FailureTrue/False等,那么我认为函数是问题,应该(如果适用)修复为返回True而是False

just personal : I prefer the not a % b way because it seems to be highly readable. 只是个人:我更喜欢not a % b方式,因为它似乎具有高度的可读性。 But now, to lower the confusion level in the code, I will use the == 0 , as it express what you expect to test exactly in a more accurate way. 但是现在,为了降低代码中的混淆程度,我将使用== 0 ,因为它表达了您希望以更准确的方式准确测试的内容。 It's the "care for debug" approach. 这是“关心调试”的方法。

0 isn't guaranteed to be a singleton so don't use is to test against it: currently C Python re-uses small integers so there is probably only one int with the value 0 plus a long if you're still on Python 2.x, and any number of float zeroes not to mention False which all compare equal to 0. Some earlier versions of Python, before it got a separate bool type used a different int zero for the result of comparisons. 0不保证是单例,所以不要使用is来测试它:当前C Python重用小整数所以如果你还在Python 2上,那么可能只有一个值为0加上long int .x,以及任何数量的浮点零,更不用说False,它们都比较等于0.一些早期版本的Python,在得到一个单独的bool类型之前,对比较结果使用了不同的int 0。

Use either == (which would be my preference) or just the not , whichever you prefer. 使用== (这将是我的偏好)或只是not喜欢,无论你喜欢什么。

A and C are both valid and very pythonic. A和C都是有效且非常pythonic。

B is not, because B不是,因为

  1. 0 semantically is not a singleton (it is in cPython, but that is an implementation detail). 0语义上不是单例(它在cPython中,但这是一个实现细节)。
  2. It will not work with float a or b . 它不适用于浮动ab
  3. It is actually possible that this will not work in some other implementation of Python. 实际上,这可能不适用于Python的其他一些实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM