[Python-ideas] Aid reiteration with new class: gfic

Terry Reedy tjreedy at udel.edu
Thu Jun 18 23:02:12 CEST 2009

Most functions that take an iterable as parameter iterate just once.
Users of the function can pass any iterable, including iterators.
The function will call iter(imput) and go.
If the user wants to iterate thru a virtual collection defined by an 
instance of an iterator class (built-in or user-defined) or generator 
function, the user must call the gf/ic with the appropriate arguments 
and pass the result.

Some functions iterate thru the input more than once. If the input is 
not an iterator, there is no problem: call iter(input) again. If the 
input is an iterator, that just returns the now-empty iterator.  Bad.

General solution:
The most general solution is for the caller to pass the result of 
list(it) instead of it. If the caller starts with only an iterator, that 
   is the only solution I know of.

Special solution:
If the caller starts with an non-iterable iterator source -- iterator 
class or generator function (which can be regarded as defining a virtual 
subclass of iterator class 'generator') -- it might be better to package 
that source and required args as an iterable.

class gfic: #untested as yet
   def __init__(self, func, *args, **kwds):
     self.func = func
     self.args = args
     self.kwds = kwds
     self.__name__ = func.__name__
   def __iter__(self):
     return self.func(*self.args, **self.kwds)

This is similar, I believe, to functools.partial except that the 
wrapping is total rather than partial and the wrapped call is in 
__iter__ instead of __call__.

Would this be a sensible addition to functools, say, or is the use case 
too rare?

Terry Jan Reedy

More information about the Python-ideas mailing list