Should I test object id in eq function

When implementing the function, __eq__I was interested to know if I need to test the identification using the keyword is. Testing equality for two different variables can be immediately evaluated by the Truefunction iswhen the variables point to the same instance of the object. If the variables point to different instances, further testing is, of course, necessary. Let's say we have it S = U, and obviously we want it to S == Ureturn True, and identity testing will speed it up.

def __eq__(self, other):
    if self is other:
        return True
    else:
        pass # do another user defined test for equality 
+4
source share
1 answer

, , , - . Python , in list s; C ( CPython), Python:

any(element is target or element == target for element in lst)

, , , , numpy.NAN is numpy.NAN - True, numpy.NAN == numpy.NAN - False. , , .

+1

Source: https://habr.com/ru/post/1695210/


All Articles