Rich Comparisons Gotcha

Luis Zarrabeitia kyrie at uh.cu
Wed Dec 10 17:58:49 EST 2008


On Sunday 07 December 2008 09:21:18 pm Robert Kern wrote:
> The deficiency is in the feature of rich comparisons, not numpy's
> implementation of it. __eq__() is allowed to return non-booleans; however,
> there are some parts of Python's implementation like list.__contains__()
> that still expect the return value of __eq__() to be meaningfully cast to a
> boolean.

list.__contains__, tuple.__contains__, the 'if' keyword...

How do can you suggest to fix the list.__contains__ implementation?

Should I wrap all my "if"s with this?:

if isinstance(a, numpy.array) or isisntance(b,numpy.array):
    res = compare_numpy(a,b)
elif isinstance(a,some_otherclass) or isinstance(b,someotherclass):
    res = compare_someotherclass(a,b)
...
else:
    res = (a == b)
if res:
   # do whatever

-- 
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie



More information about the Python-list mailing list