Why keep identity-based equality comparison?

spam.noam at gmail.com spam.noam at gmail.com
Tue Jan 10 17:21:32 EST 2006


It seems to me that both Mike's and Fuzzyman's objections were that
sometimes you want the current behaviour, of saying that two objects
are equal if they are: 1. the same object or 2. have the same value
(when it's meaningful).  In both cases this can be accomplished pretty
easily: You can do it with a try..except block, and you can write the
try...except block inside the __contains__ method.  (It's really pretty
simple: try:     return a == b except TypeError:     return a is b )
Also, Mike said that you'll need an idlist object too - and I think
he's right and that there's nothing wrong with it.  Note that while you
can easily define the current == behaviour using the proposed
behaviour, you can't define the proposed behaviour using the current
behaviour. Also note that using the current behaviour, you can't easily
treat objects that do define a meaningful value comparison, by
identity. Also note that in the cases that you do want identity-based
behaviour, defining it explicitly can result in a more efficient
program: explicit identity-based dict doesn't have to call any __hash__
and __eq__ protocols - it can compare the pointers themselves. The same
if you want to locate a specific object in a list - use the proposed
idlist and save yourself O(n) value-based comparisons, which might be
heavy.  Noam




More information about the Python-list mailing list