On Thu, Jul 25, 2019 at 05:53:47PM +0200, Antoine Pitrou wrote:
> On Wed, 24 Jul 2019 19:09:37 -0400
> David Mertz <[email protected]> wrote:
> >
> > There are various possible behaviors that might make sense, but having
> > `d.values() != d.values()` is about the only one I can see no sense in.
>
> Why? Does the following make no sense to you?
>
> >>> iter(()) == iter(())
> False
Views are not iterators, and the analogy is a poor one.
In their own way, iterators are almost as weird as NANs. (Not *quite* as
weird, since NANs break reflexivity too: x != x when x is a NAN.) But
having two iterators which clearly yield the same values in the
same order compare as unequal is weird.
("Same values in same order" includes the pair of exhausted iterator
case.)
The behaviour of iterators can be justified, and I'm not going to argue
that it should be changed. For starters iterators are not *containers*,
they are conceptually more of a process (yielding values one at a time).
But we shouldn't emulate iterator behaviour in objects which aren't like
iterators.
Views are collections. They are sized, containers (support ``in``),
and iterable:
py> v = {'a': 1, 'b': 2}.values()
py> len(v)
2
py> 2 in v
True
py> list(v)
[1, 2]
and unlike iterators, iterating over a view doesn't exhaust it.
Conceptually, equality of two values view objects should be easy (if we
don't care about efficiency of implementation). Two views are equal if
they have the same length, and each value occurs the same number of
times. Value views don't currently support the .count() method, but if
they did, we could say two value views a, b were equal if:
len(a) == len(b) and all(a.count(x) == b.count(x) for x in a)
The .count method could be implemented like this:
def count(self, x):
n = 0
for a in self:
if a == x:
n += 1
return n
So there are no conceptual problems in defining equality for value
views. Putting aside efficiency, this is easy to solve.
--
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/TSAOXOEXRIE5PXZ2DYR2KKDLTPRCHIHI/