[Python-3000] Thoughts on dictionary views
Guido van Rossum
guido at python.org
Wed Feb 21 00:25:28 CET 2007
On 2/20/07, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Raymond Hettinger wrote:
> > * Maintaining a live (self-updating) view is a bit tricky from an implementation
> > point-of-view.
> I don't understand what the alternative is. If mutating the
> underlying object doesn't affect the view, then you don't
> really have a view, just a copy of the data -- no different
> from the existing dict keys() etc.
FWIW, I didn't find the implementation tricky at all -- the views are
very small objects that simply contain a reference to the underlying
dict. All operations on the view defer to the dict one way or another.
The code in the PEP also shows how simple this is to do generically
for any underlying mapping object that implements __getitem__,
__contains__, __len__, and __iter__. (Or it will, once I am done
updating it. :-)
> If you're saying that you shouldn't be able to mutate the
> underlying object *through* the view, that's okay -- I don't
> mind if the views are read-only in some or all cases.
While the PEP has some mutability, the implementation currently has
all views be read-only, and I like this enough to want to keep it that
> > Let's make dicts as
> > simple as possible and then introduce a new collections module entry with the
> > views bells and whistles.
> If the view methods are only available on a special dict
> subclass and not on ordinary dicts, their usefulness will
> be severely crippled, so you wouldn't learn much from
> the experiment.
True. I'm also unclear on what "as simple as possible" would mean.
Perhaps delete iterkeys etc. and make keys etc. return iterators? That
was the *old* plan, which was never really challenged, and IMO it is
in every aspect inferior to the current plan.
BTW the PEP was incorrectly marked as accepted. I'll unmark it, and
remove the mutability.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000