gdbm objects not iterable?

Steve Holden steve at holdenweb.com
Wed Oct 3 16:08:53 CEST 2007


Laszlo Nagy wrote:
>   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. :-)
> 
What you would need is a method to *create and return* a generator, and 
it's the generator that should have the next() method. Otherwise you 
can't have two independent iterations over the same gdbm object (and you 
don't have a way of ensuring iteration starts at the beginning).

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline




More information about the Python-list mailing list