Rich Comparisons Gotcha
Terry Reedy
tjreedy at udel.edu
Wed Dec 10 13:54:42 EST 2008
Rasmus Fogh wrote:
> Rhamphoryncus wrote:
>> You grossly overvalue using the "in" operator on lists.
>
> Maybe. But there is more to it than just 'in'. If you do:
>>>> c = numpy.zeros((2,))
>>>> ll = [1, c, 3.]
> then the following all throw errors:
> 3 in ll, 3 not in ll, ll.index(3), ll.count(3), ll.remove(3)
> c in ll, c not in ll, ll.index(c), ll.count(c), ll.remove(c)
>
> Note how the presence of c in the list makes it behave wrong for 3 as
> well.
So do not put numpy arrays into lists without wrapping them. They were
designed and semi-optimized, by a separate community, for a specific
purpose -- numerical computation -- and not for 'playing nice' with
other Python objects.
It is a design feature of Python that people can implement specialized
objects with specialized behaviors for specialized purposes.
More information about the Python-list
mailing list