[Python-3000] Thoughts on dictionary views

Steven Bethard steven.bethard at gmail.com
Wed Feb 21 01:10:53 CET 2007

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

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.

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.

I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy

More information about the Python-3000 mailing list