[ python-Bugs-1164726 ] UserDict is not iterable

SourceForge.net noreply at sourceforge.net
Wed Mar 16 21:24:40 CET 2005


Bugs item #1164726, was opened at 2005-03-16 14:34
Message generated for change (Settings changed) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1164726&group_id=5470

Category: Python Library
Group: Python 2.4
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Kent Johnson (kjohnson)
Assigned to: Nobody/Anonymous (nobody)
Summary: UserDict is not iterable

Initial Comment:
UserDict does not directly support iteration:

>>> import UserDict
>>> ud = UserDict.UserDict()
>>> ud['a'] = 1
>>> [k for k in ud]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\Python24\lib\UserDict.py", line 17, in
__getitem__
    def __getitem__(self, key): return self.data[key]
KeyError: 0

The fix is to define __iter__ = iterkeys:
>>> class UD(UserDict.UserDict):
...   __iter__ = UserDict.UserDict.iterkeys
...
>>> ud = UD()
>>> ud['a'] = 1
>>> [k for k in ud]
['a']

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2005-03-16 15:24

Message:
Logged In: YES 
user_id=80475

This could not be done for reasons of backwards compatability.

To get what you want, use UserDict.IterableUserDict.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1164726&group_id=5470


More information about the Python-bugs-list mailing list