why does UserDict.DictMixin use keys instead of __iter__?

Raymond Hettinger vze4rx4y at verizon.net
Tue Jan 4 21:59:24 CET 2005

[Steven Bethard]
> Sorry, my intent was not to say that I didn't know from the docs that
> UserDict.DictMixin required keys().  Clearly it's documented.  My
> question was *why* does it use keys()?  Why use keys() when keys() can
> be derived from __iter__, and __iter__ IMHO looks to be a more basic
> part of the mapping protocol.

Viewed from the present, __iter__() may seem more basic.  However, it is a
recent innovation.  The keys() method, on the other hand, goes back to the
beginning.  There were no shortage of mapping-like classes defining keys() but
not __iter__().

Still, if __iter__() is provided, UserDict.DictMixin will take advantage of it.
The same is also true for __contains__(), and iteritems().

Raymond Hettinger

More information about the Python-list mailing list