- Clarify that the ordering of keys, values and items is compatible.
- Note that comparing two d_items instances could compare the underlying dicts. - Ask whether we need more of a motivation. - Observe that we could cache+reuse the keys/values/items objects.
This commit is contained in:
parent
c19fe93c56
commit
2faa29c5cf
21
pep-3106.txt
21
pep-3106.txt
|
@ -23,6 +23,7 @@ methods.
|
|||
The approach is inspired by that taken in the Java Collections
|
||||
Framework [1]_.
|
||||
|
||||
|
||||
Introduction
|
||||
============
|
||||
|
||||
|
@ -243,6 +244,7 @@ I'm using pseudo-code to specify the semantics::
|
|||
return True
|
||||
if not isinstance(other, d_items):
|
||||
return NotImplemented
|
||||
# XXX We could also just compare the underlying dicts...
|
||||
if len(self) != len(other):
|
||||
return False
|
||||
for item in self:
|
||||
|
@ -306,13 +308,32 @@ that's not feasible without copying the underlying dict. If you want
|
|||
a copy of a specific type, like list or set, you can just pass one
|
||||
of the above to the list() or set() constructor.
|
||||
|
||||
Also note that the specification implies that the order in which items
|
||||
are returned by .keys(), .values() and .items() is the same (just as
|
||||
it was in Python 2.x), because the order is all derived from the dict
|
||||
iterator (which is presumably arbitrary but stable as long as a dict
|
||||
isn't modified). This can be expressed by the following invariant::
|
||||
|
||||
list(d.items()) == list(zip(d.keys(), d.values()))
|
||||
|
||||
|
||||
Open Issues
|
||||
===========
|
||||
|
||||
Do we need more of a motivation? I would think that being able to do
|
||||
set operations on keys and items without having to copy them should
|
||||
speak for itself.
|
||||
|
||||
I've left out the implementation of various set operations. These
|
||||
could still present surprises.
|
||||
|
||||
It would be okay if multiple calls to d.keys() (etc.) returned the
|
||||
same object, since the object's only state is the dict to which it
|
||||
refers. Is this worth having extra slots in the dict object for?
|
||||
Should that be a weak reference or should the d_keys (etc.) object
|
||||
live forever once created? Strawman: probably not worth the extra
|
||||
slots in every dict.
|
||||
|
||||
Should d_values have mutating methods (pop(), clear())? Strawman: no.
|
||||
|
||||
Should d_values implement set operations (as defined for multisets).
|
||||
|
|
Loading…
Reference in New Issue