why does UserDict.DictMixin use keys instead of __iter__?

Steven Bethard steven.bethard at gmail.com
Tue Jan 4 09:22:25 EST 2005

Nick Coghlan wrote:
> Steven Bethard wrote:
>> Sorry if this is a repost -- it didn't appear for me the first time.
>> So I was looking at the Language Reference's discussion about emulating
>> container types[1], and nowhere in it does it mention that .keys() is
>> part of the container protocol.  Because of this, I would assume that to
>> use UserDict.DictMixin correctly, a class would only need to define
>> __getitem__, __setitem__, __delitem__ and __iter__.  So why does
>> UserDict.DictMixin require keys() to be defined?
> Because it's a DictMixin, not a ContainerMixin?

"Containers usually are sequences (such as lists or tuples) or mappings 
(like dictionaries)".

> .keys() is definitely part of the standard dictionary interface, and not 
> something the mixin can derive from the generic container methods.

Why is that?  Isn't keys derivable as:

def keys(self):
     return list(self)

if __iter__ is defined?


More information about the Python-list mailing list