[Python-bugs-list] [ python-Bugs-500078 ] Add built-in generator factory function

noreply@sourceforge.net noreply@sourceforge.net
Sun, 06 Jan 2002 23:19:28 -0800


Bugs item #500078, was opened at 2002-01-06 00:25
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=500078&group_id=5470

Category: Python Interpreter Core
Group: Feature Request
>Status: Deleted
Resolution: None
>Priority: 1
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Nobody/Anonymous (nobody)
Summary: Add built-in generator factory function

Initial Comment:
Add factory function "generator" to built-ins.

Used by itself, "generator" is short-hand for 
types.GeneratorType

Used with a sequence argument, it produces a generator
with the __iter__ and next() API.

def generator( iterable ):
    for i in iterable:
        yield i

g = generator( [1,2,3,4] )
h = generator( range(10) )
i = generator( xrange(10) )
j = generator( 'abcd' )

This parallels what was done with dict() and list() 
and other types.  It allows easy type checking, ala,
isinstance(j,generator).  It helps coerce arguments 
when a function expects the __iter__ and next() API.
For example:

def xzip(*iterables):
    gens = map(generator, *iterables)
    while 1:
        yield tuple( [g.next() for g in gens] )

g = xzip( [1,2,3], range(20), xrange(5), genPrimes
(10), getTimeStamp(), open("fil") )  

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

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