[issue5973] re-usable generators / generator expressions should return iterables

Jae Kwon report at bugs.python.org
Tue Jun 30 04:15:17 CEST 2009


Jae Kwon <jkwon.work at gmail.com> added the comment:

I second this feature request, and will try to get consensus in
python-ideas. 

Meanwhile, here's a sample workaround.

>>> def gen2iterable(genfunc):
...     def wrapper(*args, **kwargs):
...         class _iterable(object):
...             def __iter__(self):
...                 return genfunc(*args, **kwargs)
...         return _iterable()
...     return wrapper
... 
>>> 
>>> @gen2iterable
... def foo():
...   for i in range(10):
...     yield i
... 
>>> a = foo()
>>> 
>>> max(a)
9
>>> max(a)
9
>>> def secondmax(iterable):
...     """return the second largest value in iterable"""
...     m = max(iterable)
...     return max(i for i in iterable if i<m)
... 
>>> secondmax(a)
8

----------
nosy: +Jae

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5973>
_______________________________________


More information about the Python-bugs-list mailing list