python generators are good replacements for lists in most cases expect where I would like to check for empty condition which is not possible with plain generators. I am trying t
Use itertools.tee to implement the nonzero test, and simply cache it on creation:
from itertools import tee
class NonZeroIterable(object):
def __init__(self, iterable):
self.__iterable, test = tee(iter(iterable))
try:
test.next()
self.__nonzero = True
except StopIteration:
self.__nonzero = False
def __nonzero__(self):
return self.__nonzero
def __iter__(self):
return self.__iterable
Little demo:
>>> nz = NonZeroIterable('foobar')
>>> if nz: print list(nz)
...
['f', 'o', 'o', 'b', 'a', 'r']
>>> nz2 = NonZeroIterable([])
>>> if not nz2: print 'empty'
...
empty
This version of the NonZeroIterable caches the flag; it thus only tells you if the iterator was non-empty at the start. If you need to be able to test the iterable at other points in it's lifecycle, use Sven's version instead; there the __nonzero__
flag will tell you after every iteration if there are more items to come still.
Your sample code is way too simple and not a good argument for your usecase; you first test for non-emptyness (which potentially iterates over the input list to seach for an odd number) but then exhaust the whole iterator anyway. The following code would be just as efficient and wouldn't require you to invent ways to break python idioms:
def print_odd(odd_nums):
odd_nums = list(odd_nums)
if odd_nums:
print "odd numbers found", odd_nums
else:
print "No odd numbers found"