when是`==`运算符不等于`is`运算符? (蟒蛇) [英] When is the `==` operator not equivalent to the `is` operator? (Python)

查看:146
本文介绍了when是`==`运算符不等于`is`运算符? (蟒蛇)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我可以使用 == 运算符来比较所有的本地数据类型(整数,字符串,布尔值,浮点数等)集和包含本机数据类型的字典。在这些情况下, == 操作符检查两个对象是否相等。但在其他一些情况下(试图比较我创建的类的实例) == 运算符只是检查两个变量是否引用同一个对象(因此在这些情况下 == 运算符等效于运算符)



strong>我的问题是: == 操作符何时不仅仅比较身份?



编辑:我使用Python 3

解决方案

在Python中, == 运算符是根据魔法 __eq __ ,默认情况下通过身份比较实现它。但是,您可以覆盖方法,以提供自己的对象等式概念。注意,如果你这样做,你通常还会重写至少 __ ne __ (它实现!= 运算符) __ hash __ ,它计算了实例的哈希码。



我发现它非常有用,以使我的 __ eq __ 实现符合在Java语言中为实现 equals 方法,即:




  • 它是反身的:对于任何非空引用值x,x.equals(x)应该返回true。 / li>
  • 它是对称的:对于任何非空引用值x和y,如果且仅当y.equals(x)返回true,x.equals(y)才会返回true。 >
  • 它是传递性的:对于任何非空参考值x,y和z,如果x.equals(y)返回true,而y.equals(z)返回true,那么x.equals

  • 它是一致的:对于任何非空引用值x和y,x.equals(y)的多个调用始终返回true或始终返回false,提供

  • 对于任何非空引用值x,x.equals(null)应返回false。 c> $ None ,但在Python中的规则在Java中并不容易。


    I noticed I can use the == operator to compare all the native data types (integers, strings, booleans, floating point numbers etc) and also lists, tuples, sets and dictionaries which contain native data types. In these cases the == operator checks if two objects are equal. But in some other cases (trying to compare instances of classes I created) the == operator just checks if the two variables reference the same object (so in these cases the == operator is equivalent to the is operator)

    My question is: When does the == operator do more than just comparing identities?

    EDIT: I'm using Python 3

    解决方案

    In Python, the == operator is implemented in terms of the magic method __eq__, which by default implements it by identity comparison. You can, however, override the method in order to provide your own concept of object equality. Note, that if you do so, you will usually also override at least __ne__ (which implements the != operator) and __hash__, which computes a hash code for the instance.

    I found it very helpful, even in Python, to make my __eq__ implementations comply with the rules set out in the Java language for implementations of the equals method, namely:

    • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
    • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
    • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
    • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
    • For any non-null reference value x, x.equals(null) should return false.

    the last one should probably replace null with None, but the rules are not as easy here in Python as in Java.

    这篇关于when是`==`运算符不等于`is`运算符? (蟒蛇)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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