Python中-1和False有区别吗?
我一直认为在条件中使用 -1 总是与写入 False (布尔值)相同。但从我的代码中,我得到了不同的结果:
使用 True 和 False:
def count(sub, s):
count = 0
index = 0
while True:
if string.find(s, sub, index) != False:
count += 1
index = string.find(s, sub, index) + 1
else:
return count
print count('nana', 'banana')
结果:解释器需要很长时间才能响应。
使用 1 和 -1:
def count(sub, s):
count = 0
index = 0
while 1:
if string.find(s, sub, index) != -1:
count += 1
index = string.find(s, sub, index) + 1
else:
return count
print count('nana', 'banana')
结果:1
为什么使用 -1 和 1 给出正确的结果,而使用布尔值 True 和 False 则不能给出正确的结果?
I have always thought that using -1 in a condition is alway the same as the writing False (boolean value). But from my code, I get different results:
Using True and False:
def count(sub, s):
count = 0
index = 0
while True:
if string.find(s, sub, index) != False:
count += 1
index = string.find(s, sub, index) + 1
else:
return count
print count('nana', 'banana')
Result: Takes to long for interpreter to respond.
Using 1 and -1:
def count(sub, s):
count = 0
index = 0
while 1:
if string.find(s, sub, index) != -1:
count += 1
index = string.find(s, sub, index) + 1
else:
return count
print count('nana', 'banana')
Result: 1
Why does using -1 and 1 give me the correct result whereas using the bool values True and False do not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
False 的类型为 bool,它是 int 的子类型,其值为 0。
在 Python 中,
False
类似于使用 0,而不是 -1False is of type bool, which is a sub-type of int, and its value is 0.
In Python,
False
is similar to using 0, not -1出于历史和灵活性的原因,相等性和转换为布尔值以进行真值测试之间存在差异:
There's a difference between equality and converting to a boolean value for truth testing, for both historical and flexibility reasons:
1)不。它从来都不一样,我无法想象为什么你会这么想,更不用说一直这么想了。除非由于某种原因你只使用过
if
和string.find
或其他东西。2) 您一开始就不应该使用
string
模块。直接引用文档:因此,我们使用
str
类本身的.find
方法('foobar'
和'foo'
所属的类);因为我们有该类的对象,所以我们可以进行绑定方法调用,因此:'foobar'.find('foo')
。3) 字符串的
.find
方法返回一个数字,告诉您在哪里找到子字符串(如果找到)。如果未找到子字符串,则返回 -1。在这种情况下它不能返回 0,因为这意味着“在开头找到”。4)
False
将比较等于0
。值得注意的是,Python 实际上将其bool
类型实现为int
的子类。5) 无论您使用什么语言,都不应该与布尔文字进行比较。很简单,
x == False
或等效内容是不正确的。它不会让你变得更加清晰,反而创造了犯错误的机会。你永远不会用英语说“如果真的在下雨,我需要一把雨伞”,即使这在语法上是正确的。没有意义;这并不比显而易见的“如果下雨了,我需要一把雨伞”更礼貌,也更明确。
如果您想将值用作布尔值,则将其用作布尔值。如果您想使用比较的结果(即“该值是否等于-1?”),则执行比较。
1) No. It is never the same, and I can't imagine why you would have ever thought this, let alone always thought it. Unless for some reason you had only ever used
if
withstring.find
or something.2) You shouldn't be using the
string
module in the first place. Quoting directly from the documentation:So instead of
string.find('foobar', 'foo')
, we use the.find
method of thestr
class itself (the class that'foobar'
and'foo'
belong to); and since we have objects of that class, we can make bound method calls, thus:'foobar'.find('foo')
.3) The
.find
method of strings returns a number that tells you where the substring was found, if it was found. If the substring wasn't found, it returns -1. It cannot return 0 in this case, because that would mean "was found at the beginning".4)
False
will compare equal to0
. It is worth noting that Python actually implements itsbool
type as a subclass ofint
.5) No matter what language you are using, you should not compare to boolean literals.
x == False
or equivalent is, quite simply, not the right thing to write. It gains you nothing in terms of clarity, and creates opportunities to make mistakes.You would never, ever say "If it is true that it is raining, I will need an umbrella" in English, even though that is grammatically correct. There is no point; it is not more polite nor more clear than the obvious "If it is raining, I will need an umbrella".
If you want to use a value as a boolean, then use it as a boolean. If you want to use the result of a comparison (i.e. "is the value equal to -1 or not?"), then perform the comparison.
string.find
不会返回布尔值,因此string.find('banana', 'nana', index)
将永远返回0
(False
) 无论值如何索引
。您的示例只是重复:
-1
版本之所以有效,是因为它正确解释了string.find
的返回值!string.find
doesn't return a boolean sostring.find('banana', 'nana', index)
will NEVER return0
(False
) regardless of the value ofindex
.Your example simply repeats:
The
-1
version works because it correctly interprets the return value ofstring.find
!