[Python-Dev] Re: Comparing dict.values()

2019年7月24日 14:47:16 -0700

On Wed, Jul 24, 2019 at 05:30:19PM -0000, Brett Cannon wrote:
> When I saw this I thought, "it should be like `set(d1.values()) == 
> set(d2.values())`", but has been pointed out there's no guarantee that 
> all values will be hashable.
The hashability requirement for sets is, in a sense, an implementation 
detail. It might be a requirement for sets in Python the language, 
but its not a requirement for abstract "sets of values".
E.g. Java includes a standard TreeSet which doesn't require hashability
https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
In this case, they need to be multisets, since 
{'a': 1, 'b': 2, 'c': 1}.values() != {'a': 1, 'b': 2, 'c': 2}.values()
> After that I have no expectation since order isn't guaranteed.
> 
> I think this is one of those cases where it's superficially surprising 
> when you don't think about all the ramifications, but once you 
> understand the complexity of the problem then it becomes more clear 
> that it isn't straight-forward.
Nobody said it was straight-forward, particularly if we want guaranteed 
efficient comparisons in both time and space at the same time.
Brett, I feel that you are dismissing this thread as "not thinking 
through the ramifications" without reading it through, because I'm 
pretty sure that we have thought through the ramifications in a lot more 
detail than your dismissal justifies.
Let's start with the minimal change we have suggested: that two views 
should be considered equal if they both belong to the same dict.
assert d.values() == d.values()
Currently that assertion fails. Should it? Putting aside the convenience 
of "do nothing, just inherit the object.__eq__ behaviour" do you think 
that the current behaviour is *correct*?
(By correct I mean in the technical sense that if we were writing a 
functional spec for views, we would actively desire two views of the 
same dict to be unequal.)
> To me a doc update for dict.values() stating that the iterator can't 
> be compared and a brief mention as to why would be the best solution 
> for this.
We're not talking about comparing iterators. We're talking about 
comparing views into a dict. That's a difference that makes all the 
difference.
-- 
Steven
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/UPFBDHOHFSFLDF5KO3QZE6RH3C2GXE6B/

Reply via email to