Rich Comparisons Gotcha
robert.kern at gmail.com
Mon Dec 8 23:13:12 CET 2008
Terry Reedy wrote:
> Rasmus Fogh wrote:
>> much, though, just to get code like this to work as intended:
>> print ('x is present: ', x in alist)
> Even if rich comparisons as you propose, the above would *still* not
> necessarily work. Collection classes can define a __contains__ that
> overrides the default and that can do anything, though True/False is
No, it's actually required.
In : class A(object):
def __contains__(self, other):
In : a = A()
In : 1 in a
Okay, so it will coerce to True/False for you, but unlike rich comparisons, the
return value must be interpretable as a boolean.
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list