[Python-3000] Thoughts on dictionary views

Delaney, Timothy (Tim) tdelaney at avaya.com
Wed Feb 21 01:33:42 CET 2007

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

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

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.

Tim Delaney

More information about the Python-3000 mailing list