Are there any list comparison optimizations in Python?

Steve Holden sholden at holdenweb.com
Fri Nov 16 10:26:08 EST 2001


"Skip Montanaro" <skip at pobox.com> wrote ...
>
>     Huaiyu> So my question really is this: Is there any major practical
use
>     Huaiyu> for an object to be unequal to itself?  Would it be better to
>     Huaiyu> just define (a==b) to take a shortcut of (a is b), always?
>
> The "is" operator is defined to just do pointer comparisons.  Here are
just
> three different ways to generate objects that are equal but not "is":
>
> >>> a = 457
> >>> b = 99+358
> >>> a is b
> 0
> >>> a == b
> 1
>
> >>> a = "now is the time..."
> >>> b = " ".join(a.split())
> >>> a is b
> 0
> >>> a == b
> 1
>
> >>> a = [1]
> >>> b = [1]
> >>> a == b
> 1
> >>> a is b
> 0
>
> In the first two cases perhaps Python could make sure that a and b shared
> references to the same object, but I think that would require the
> interpreter to do a lot more work just to maintain that consistency.
>
> In the last case you most definitely don't want "is" and "==" to be
> synonyms.  That would imply that a and b referred to the same object.  If
> they did, any list-modifying methods applied using one name would be
> reflected when that object was viewed using the other name, which is
> generally not what you want for mutable objects.
>
Skip, I think you just turned this discussion on its head. THe question you
responded to was not "should equal values compare true with 'is'", but "is
it useful for 'a != b' and 'a is b' to be true for the same a and b".

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list