[Python-3000] Thoughts on dictionary views

Guido van Rossum guido at python.org
Wed Feb 21 01:45:24 CET 2007

On 2/20/07, Delaney, Timothy (Tim) <tdelaney at avaya.com> wrote:
> Steven Bethard wrote:
> > The advantage is only in what you have to explain about the object. In
> > the former case, you can simply say "it's an iterator over the keys"
> > and they can understand it with their existing knowledge of iterators.
> "it's an iterator over the keys"
> They use their knowledge of iterators (a standard concept in Python
> 2.2+).
> > On the other hand, when they're told "it's a dict key view object",
> > they can't use any existing knowledge. They have to go and look up the
> "it's a set view of the keys"
> They use their knowledge of sets (a standard concept in Python 2.3+) and
> views (a standard concept in Python 2.6+).
> The standard concept of a view will be something like:
> A view is a lightweight object that implements an interface by
> delegating to an underlying object. The underlying object cannot be
> changed through the view, but could be changed directly, in which case
> the view will reflect the new contents of the object.
> Note that some changes to the underlying object may invalidate the view,
> in which case using it will throw an exception.

No, this only invalidates an in-progress iterator.

> Note also that there is nothing preventing someone from creating a
> view-like class that allows changing the underlying object through it,
> but such a class should probably not be described as a view.

You can also think of dict views as a straightforward application of
the GoF adapter pattern.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-3000 mailing list