Rich Comparisons Gotcha
Rasmus Fogh
rhf22 at mole.bio.cam.ac.uk
Wed Dec 10 09:49:12 EST 2008
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.
> It's far more
> common to use a dict or set for containment tests, due to O(1)
> performance rather than O(n). I doubt the numpy array supports
> hashing, so an error for misuse is all you should expect.
Indeed it doees not. So there is not much to be gained from modifying
equality comparison with sets/dicts.
> In the rare case that you want to test for identity in a list, you can
> easily write your own function to do it upfront:
> def idcontains(seq, obj):
> for i in seq:
> if i is obj:
> return True
> return False
Again, you can code around any particular case (though wrappers look like
a more robust solution). Still, why not get rid of this wart, if we can
find a way?
---------------------------------------------------------------------------
Dr. Rasmus H. Fogh Email: r.h.fogh at bioc.cam.ac.uk
Dept. of Biochemistry, University of Cambridge,
80 Tennis Court Road, Cambridge CB2 1GA, UK. FAX (01223)766002
More information about the Python-list
mailing list