gdbm objects not iterable?

Laszlo Nagy gandalf at shopzeus.com
Wed Oct 3 09:37:15 EDT 2007


  Hi All,

gdbm objects have a "firstkey" and a "nextkey" method. So if I want 
iterate through the keys (which I often do) then I have to do this:

def itergdbmkeys(gdbm_obj):
    key = gdbm_obj.firstkey()
    if key is None:
        raise StopIteration
    yield key
    while True:
        key = gdbm_obj.nextkey(key)
        if key is None:
            raise StopIteration
        yield key

Then I can do this:

for key in itergdbmkeys(gdbm_obj):
    process(key,gdbm_obj)

I wonder why we do not have a "next()" method, which could return an 
iterator instantly. I don't think that it would be harmful, but 
definitely it would be useful. Then we could do this:

for key in gdbm_obj:
    process(key,obj)

I know that this is a very small change, but it can make gdbm objects 
look more like dictionaries.

Please make comments. :-)

   Laszlo




More information about the Python-list mailing list