Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to check for identity in __eq__?

When implementing a custom equality function for a class, does it make sense to check for identity first? An example:

def __eq__(self, other):
    return (self is other) or (other criteria)

This interesting is for cases when the other criteria may be more expensive (e.g. comparing some long strings).

like image 200
Björn Pollex Avatar asked Jul 05 '10 13:07

Björn Pollex


People also ask

What is the __ eq __ method?

Summary. Implement the Python __eq__ method to define the equality logic for comparing two objects using the equal operator ( == )

Why we are using this == in Python?

The == operator helps us compare the equality of objects. The is operator helps us check whether different variables point towards a similar object in the memory. We use the == operator in Python when the values of both the operands are very much equal.

What is the difference between == & is?

== is for value equality. It's used to know if two objects have the same value. is is for reference equality. It's used to know if two references refer (or point) to the same object, i.e if they're identical.

What keyword can be used to check whether two values have the same identity?

Comparing Equality With the Python == and != Use the equality operators == and != if you want to check whether or not two objects have the same value, regardless of where they're stored in memory.


2 Answers

It may be a perfectly reasonable shortcut to check for identity first, and in equality methods good shortcuts (for both equality and non equality) are what you should be looking for so that you can return as soon as possible.

But, on the other hand, it could also be a completely superfluous check if your test for equality is otherwise cheap and you are unlikely in practice to be comparing an object with itself.

For example, if equality between objects can be gauged by comparing one or two integers then this should be quicker than the identity test, so in less than the time it would take to compare ids you've got the whole answer. And remember that if you check the identities and the objects don't have the same id (which is likely in most scenarios) then you've not gained anything as you've still got to do the full check.

So if full equality checking is not cheap and it's possible that an object could be compared against itself, then checking identity first can be a good idea.


Note that another reason the check isn't done by default is that it is quite reasonable (though rare) for objects with equal identities to compare as non equal, for example:

>>> s = float('nan')
>>> s == s
False
like image 50
Scott Griffiths Avatar answered Sep 19 '22 06:09

Scott Griffiths


necesarry: no

does it make sense: sure, why not?

No such check is done by default, as you can see here:

class bad(object):
    def __eq__(self, other):
        return False

x = bad()
print x is x, x==x # True, False
like image 20
Jochen Ritzel Avatar answered Sep 19 '22 06:09

Jochen Ritzel