Serhiy Storchaka wrote:
> 23.07.19 23:59, Kristian Klette пише:
> > During the sprints after EuroPython, I made an
> > attempt at adding support for
> > comparing the results from .values() of two dicts.
> > Currently the following works as expected:
> > d = {'a': 1234}
> > 
> > d.keys() == d.keys()
> > d.items() == d.items()
> > 
> > but d.values() == d.values() does not return the expected
> > results. It always returns False. The symmetry is a bit off.
> > Is it expected to you that iter(d) == iter(d) returns False?
> By default the equality operator returns True when and only when 
> operands are identical. It is expected. Some objects (like numbers or 
> strings) can override the default behavior and implement different rules 
> for equality.
> > I'd argue that Python should compare the values as
> > expected here,
> > or if we don't want to encourage that behaviour, maybe we should
> > consider raising an exception.
> > Returning just False seems a bit misleading.
> > What the rule for equality do you propose? What is the use case for it?
> If you want to compare dict value views as ordered sequences, it can be 
> surprised that d1.values() != d2.values() when d1 == d2. It will
> be 
> inconsistent with orderless comparison of keys() and items(). If
> you 
> want to compare them as unordered sequences, the computation complexity 
> of the operation will be quadratic.
> Note also, that while in Python 2 always d.values() == d.values(), it 
> is possible that d1.keys() != d2.keys() and d1.values() != 
> d2.values() when d1 == d2. Python 3 is more consistent.

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. 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.

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.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/PIFGKWEXP2LFEPCJJLVJAXIL3YCKVYCT/

Reply via email to