[Python-ideas] Fwd: Why do equality tests between OrderedDict keys/values views behave not as expected?

Emanuel Barry vgr255 at live.ca
Thu Dec 17 17:35:35 EST 2015


I may or may not have gotten this right, but I think he's asking "should that be the case?", not "is that the case?" - It is the case, but should it? I believe so. For one of my projects, I have a more complex mapping type, which under the hood is just an ODict that I do various operations on, and sometimes delegate to - for example, for equality tests against another instance of the same class, I just compare the underlying ODs. In that last case, I most certainly don't want two non-equal classes to compare equal!
I'll ask the obvious question instead: "Should two lists compare unequal if they have the same items but not the same order?"
However, the answer you want is "If I care about insertion and iteration order, there's a good chance I also care about that order when comparing, too"
If you explicitly don't care about the order, it's quite easy to do so:
>>> import collections>>> a=collections.OrderedDict([("spam", "eggs"), ("foo", "bar")])>>> b=collections.OrderedDict([("foo", "bar"), ("spam", "eggs")])>>> a == bFalse>>> dict.items(a) == dict.items(b)True
-Emanuel
> From: leewangzhong+python at gmail.com
> Date: Thu, 17 Dec 2015 15:38:21 -0500
> To: guido at python.org
> Subject: Re: [Python-ideas] Fwd: Why do equality tests between OrderedDict keys/values views behave not as expected?
> CC: python-ideas at python.org
> 
> (Note: This is already the case.
> https://docs.python.org/3/library/collections.html#collections.OrderedDict
> """
> Equality tests between OrderedDict objects are order-sensitive and are
> implemented as list(od1.items())==list(od2.items()). Equality tests
> between OrderedDict objects and other Mapping objects are
> order-insensitive like regular dictionaries. This allows OrderedDict
> objects to be substituted anywhere a regular dictionary is used.
> """
> 
> So you're asking whether to deprecate this behavior?)
> 
> On Thu, Dec 17, 2015 at 2:55 PM, Guido van Rossum <guido at python.org> wrote:
> > So I think that not using the __eq__ method of the keys or values is wrong
> > (dicts do use it), but there's a philosophical question: if two OrderedDicts
> > have the same key/value pairs in a different order, should they be
> > considered equal or not? (Only when we've answered this can we answer the
> > question about keys/values/items).
> >
> > I'm not a frequent user of OrderedDict, so I don't have a good intuition,
> > unfortunately.
> >
> > --
> > --Guido van Rossum (python.org/~guido)
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151217/b121dbf7/attachment.html>


More information about the Python-ideas mailing list