How do i generate a tuple from a generator ?

Emile van Sebille emile at fenx.com
Fri May 15 17:03:31 EDT 2009


On 5/15/2009 1:58 PM eye zak said...
> Hi,
> 
> I have a situation where i want a tuple from a generator/sequence
> comprehension.
> I am wondering, is this possible ? or is this just a bad idea all together?
> 
> class iterator(object):
>     __slots__ = ('__iters',)
> 
>     def __init__(self,*args):
>         assert len(args) != 0
>         self.__iters = tuple( __builtins__.iter(arg) for arg in args )
> 
>     def __iter__(self,*args):
>         return self
> 
>     def next(self):
>         return [ i.next() for i in self.__iters ]    # HERE I WANT A TUPLE
           return tuple(...)
Emile

> 
> This can also be accomplished with a generator function, but requires
> while True loop. is that better ?
> 
> Isaac




More information about the Python-list mailing list