I recently found out that python has a special value NotImpemented to be used with respect to binary special methods to indicate that some operation has not been implemented.
The peculiar about this is that when checked in a binary situation it is always equivalent to True
.
For example using io.BytesIO
(which is a case where __eq__
in not implemented for example) for two objects in comparison will virtually return True
. As in this example (encoded_jpg_io1
and encoded_jpg_io2
are objects of the io.BytesIO
class):
if encoded_jpg_io1.__ne__(encoded_jpg_io2):
print('Equal')
else:
print('Unequal')
Equal
if encoded_jpg_io1.__eq__(encoded_jpg_io2) == True:
print('Equal')
else:
print('Unequal')
Unequal
Since the second style is a bit too verbose and normally not prefered (even my pyCharm suggests to remove the explicit comparison with True
) isn't a bit tricky behavior? I wouldn't have noticed it if I haven't explicitly print the result of the Boolean operation (which is not Boolean in this case at all).
I guess suggesting to be considered False
would cause the same problem with __ne__
so we arew back to step one.
So, the only way to check out for these cases is by doing an exact comparison with True
or False
in the opposite case.
I know that NotImpemented
is preferred over NotImplementedError
for various reasons so I am not asking for any explanation over why this matter.