的Python:假比0 [英] Python: False vs 0
问题描述
在PHP中使用 ===
符号来测试 TRUE
或 FALSE
从 1
不同或 0
。
In PHP you use the ===
notation to test for TRUE
or FALSE
distinct from 1
or 0
.
例如如果为FALSE == 0
收益 TRUE
,如果为FALSE === 0
收益 FALSE
。因此,在基地0做字符串搜索时,如果有问题的子串的位置是正确的在你获得 0
的PHP可以从 FALSE区分初
。
For example if FALSE == 0
returns TRUE
, if FALSE === 0
returns FALSE
. So when doing string searches in base 0 if the position of the substring in question is right at the beginning you get 0
which PHP can distinguish from FALSE
.
有没有在Python这样的手段?
Is there a means of doing this in Python?
推荐答案
在Python中,
-
的
是
身份运营商测试(假的就是假
,0是不是假
)。
The
is
operator tests for identity (False is False
,0 is not False
).
的 ==
运营商测试对于逻辑相等(因此 0 ==虚假
)。
The ==
operator which tests for logical equality (and thus 0 == False
).
从技术上讲这些都不是完全等同于PHP的 ===
,其中比较合理的平等和类型 - 在Python中,这会是 A == b和类型(一)的类型是(b)
。
Technically neither of these is exactly equivalent to PHP's ===
, which compares logical equality and type - in Python, that'd be a == b and type(a) is type(b)
.
之间的一些其他方面的差异是
和 ==
:
-
{} == {}
,但{}不是{}
(出于同样也是如此列表和其他可变类型) - 但是,如果
A = {}
,那么A是一个
(因为在这种情况下,它是一个参考相同的实例)
{} == {}
, but{} is not {}
(and the same holds true for lists and other mutable types)- However, if
a = {}
, thena is a
(because in this case it's a reference to the same instance)
-
一* 255是不是a* 255
,但一* 20一个* 20
在大多数实现中,由于到Python如何处理字符串实习。这种行为不能保证,不过,你可能不应该使用是
在这案例。一* 255 ==是* 255
键,几乎总是使用正确的比较。
"a"*255 is not "a"*255"
, but"a"*20 is "a"*20
in most implementations, due to how Python handles string interning. This behavior isn't guaranteed, though, and you probably shouldn't be usingis
in this case."a"*255 == "a"*255
and is almost always the right comparison to use.
-
12345 12345
,但12345不是12345 + 1 - 1
在大多数的实现,与此类似。您pretty多少总是想用平等对于这些情况。
12345 is 12345
but12345 is not 12345 + 1 - 1
in most implementations, similarly. You pretty much always want to use equality for these cases.
这篇关于的Python:假比0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!