[Python-3000] Thoughts on dictionary views

Guido van Rossum guido at python.org
Wed Feb 21 01:17:55 CET 2007


On 2/20/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> On 2/20/07, Guido van Rossum <guido at python.org> wrote:
> > On 2/20/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> > > Just to clarfiy, you're suggesting that we still change .keys()
> > > .values() and .items() to iterators, right?
> >
> > But this isn't really easier to explain to noobs than views, is it?
> > What's the advantage of
> >
> > >>> {}.keys()
> > <dictionary-keyiterator object at 0xb7f82f60>
> > >>>
> >
> > over
> >
> > >>> {}.keys()
> > <dict_keys object at 0xb7fb6540>
> > >>>
> >
> > ???
>
> No advantage at the interactive prompt of course. ;-)
>
> 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.

Uhm, you gotta be kidding. You don't seriously expect noobs to have a
priori understanding if iterators do you? Those most likely come
*after* dict views.

> And if they don't know what iterators are, once they learn about them
> for this case, they'll also know how iterators work in other
> situations, e.g. list iterators, set iterators, deque iterators, etc.

Most of which one rarely needs to know about. In fact, i'd say that if
it wasn't for dict.iterkeys() we could probably hide iterators quite
effectively for a long time from noobs.

> 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
> API for what exactly a dict key view object does. And once they've
> learned what API a dict key view object supports, that knowledge is
> not really helpful in any new situations. They won't see key views on
> lists, sets or deques, for example.

But they will see them (I hope) on other mappings.

> So it's mainly about keeping the mental footprint small. Knowing how
> iterators work is a useful bit of knowledge that is widely applicable
> across a variety of Python objects. Knowing how the various dict views
> work is not so generally useful.

Since they mostly behave like sets (that you can't mutate directly)
they should be very low conceptual overhead. (Raymond already remarked
on the success of the set API and I wholeheartedly agree.)

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


More information about the Python-3000 mailing list