的Python:假比0 [英] Python: False vs 0

查看:94
本文介绍了的Python:假比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 = {}, then a 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 using is 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 but 12345 is not 12345 + 1 - 1 in most implementations, similarly. You pretty much always want to use equality for these cases.

这篇关于的Python:假比0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆