Trade Off

supercalifragilisticexpialidocious

NotImplemented-in-python

It’s best to return NotImplemented in magic method(e.g. __eq__, __ne__, etc), see here for details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# example.py

class A(object):
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        if isinstance(other, A):
            print('Comparing an A with an A')
            return other.value == self.value
        if isinstance(other, B):
            print('Comparing an A with a B')
            return other.value == self.value
        print('Could not compare A with the other class')
        return NotImplemented

class B(object):
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        if isinstance(other, B):
            print('Comparing a B with another B')
            return other.value == self.value
        print('Could not compare B with the other class')
        return NotImplemented

a is instance of ClassA, and b is instance of ClassB, if we invoke a == b, we will get the result, True or False, but if we invoke b == a, we will also get the result! Because of we return NotImplemented in Class B’s __eq__ method, runtime could invoke Class A’s __eq__, that method can compare A and B.

Comments