Short answer: No , but there are other forms of generators.
A / loop - loop structure : it does not emit values and, therefore, is not a generator. forwhile
However, there are other ways to build generators .
yield - , , :
def some_generator(xs):
for x in xs:
if x:
yield x
, :
(x for x in xs if x)
, python-3.x range(..), map(..), filter(..) .
, , ( ):
class some_generator(object):
def __init__(self, xs):
self.n = n
self.idx = 0
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
while self.num < len(self.xs) and not self.xs[self.num]:
self.num += 1
if self.num < len(self.xs):
res = self.xs[self.num]
self.num += 1
return res
else:
raise StopIteration()