[Tutor] iteritems() vs items()

Tim Johnson tim at johnsons-web.com
Sat Nov 12 21:12:08 CET 2005

I need to get up to speed on iterators. I learned python 1.5~ via
Alan G's book ...
For an example, I've written a subclass of dict where keys are kept in
a ordered fashion is a list called __keys:

#Here is my items function:
    def items(self):
    """ Return all pairs in order of addition"""
    return [(key,self.__dict[key]) for key in self.__keys]

#And here is my iteritems function (currently does exactly the same thing)
    def iteritems(self):
        """ At this implementation, does exactly the same thing as 
        method items()"""
        for key in self.__keys:
            yield (key,self.__dict[key])

I would welcome examples, comments, or pointers to docs that 
illustrate how I may use iteritems to extend functionality for
this class.

NOTE: An important thing about this class, is that iteration thru
members must be done in the same manner in which key/value pairs were
I'm currently using Python 2.3.4 on both workstation and internet
servers and must keep backward compatible to that venue.


Tim Johnson <tim at johnsons-web.com>

More information about the Tutor mailing list