Iterating over dictionaries and objects in general

[Followup to a discussion on psa-members about iterating over dictionaries without creating intermediate lists] Jim Fulton wrote:
Just wanted to verify my understanding ;-)
Ok, so the "risk" is under user control. Fine with me...
I think I'll give such an iterator a spin. Would be a nice extension to mxTools. BTW, a generic type slot for iterating over types would probably be a nice feature too. The type slot could provide hooks of the form it_first, it_last, it_next, it_prev which all work integer index based, e.g. in pseudo code: int i; PyObject *item; /* set up i and item to point to the first item */ if (obj.it_first(&i,&item) < 0) goto onError; while (1) { PyObject_Print(item); /* move i and item to the next item; an IndexError is raised in case there are no more items */ if (obj.it_next(&i,&item) < 0) { PyErr_Clear(); break; } } These slots would cover all problem instances where iteration over non-sequences or non-uniform sequences (i.e. sequences like objects which don't provide konvex index sets, e.g. 1,2,3,6,7,8,11,12) is required, e.g. dictionaries, multi-segment buffers -- Marc-Andre Lemburg ______________________________________________________________________ Y2000: 127 days left Business: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/
participants (1)
-
M.-A. Lemburg