Python 如何部分使用可迭代生成器(没有 `next`)?
Python how to partially consume an iterable generator (without `next`)?
我有一个 class 充当可迭代生成器(根据 ),我想使用 for
循环部分使用它。
我不能使用 next
(比如 Python -- consuming one generator inside various consumers),因为第一个部分消耗使用了一个只接受迭代器的库。如何从库函数停止的地方开始继续使用生成器?
(相关:Pause Python Generator, )
class gen(): #
def __iter__(self):
for i in range(20):
yield i
# I want to partially consume in the first loop and finish in the second
my_gen_2 = gen()
for i in my_gen_2: # imagine this is the internal implementation of the library function
print(i)
if i > 10: # the real break condition is when iterfzf recieves user input
break
for i in my_gen_2: # i'd like to process the remaining elements instead of starting over
print('p2', i)
# the confusion boils down to this
my_gen = gen()
for i in my_gen:
print(i) # prints 1 through 20 as expected
for i in my_gen:
print('part two', i) # prints something, even though the generator should have been "consumed"?
每次循环中的迭代生成器,都会得到一个新的迭代器。
例如:
class gen(): #
def __init__(self):
self.count = 0
def __iter__(self):
self.count += 1
print("Hallo iter {0}".format(self.count))
yield self.count
my_gen = gen()
>>> for i in my_gen:
... pass
Hallo iter 1
>>> for i in my_gen:
... pass
Hallo iter 2
如果您想使用旧的迭代器,只需使用 gen().__iter__()
>>> my_gen = gen().__iter__()
>>> for i in my_gen:
... pass
Hallo iter 1
>>> for i in my_gen:
... pass
正如@napuzba 指出的那样,__iter__
returns 每次使用时都是一个全新的生成器。相反,将状态存储在 self
:
class gen():
def __init__(self):
self.count = 0
def __iter__(self):
while self.count < 20:
self.count += 1
yield self.count
我有一个 class 充当可迭代生成器(根据 for
循环部分使用它。
我不能使用 next
(比如 Python -- consuming one generator inside various consumers),因为第一个部分消耗使用了一个只接受迭代器的库。如何从库函数停止的地方开始继续使用生成器?
(相关:Pause Python Generator,
class gen(): #
def __iter__(self):
for i in range(20):
yield i
# I want to partially consume in the first loop and finish in the second
my_gen_2 = gen()
for i in my_gen_2: # imagine this is the internal implementation of the library function
print(i)
if i > 10: # the real break condition is when iterfzf recieves user input
break
for i in my_gen_2: # i'd like to process the remaining elements instead of starting over
print('p2', i)
# the confusion boils down to this
my_gen = gen()
for i in my_gen:
print(i) # prints 1 through 20 as expected
for i in my_gen:
print('part two', i) # prints something, even though the generator should have been "consumed"?
每次循环中的迭代生成器,都会得到一个新的迭代器。 例如:
class gen(): #
def __init__(self):
self.count = 0
def __iter__(self):
self.count += 1
print("Hallo iter {0}".format(self.count))
yield self.count
my_gen = gen()
>>> for i in my_gen:
... pass
Hallo iter 1
>>> for i in my_gen:
... pass
Hallo iter 2
如果您想使用旧的迭代器,只需使用 gen().__iter__()
>>> my_gen = gen().__iter__()
>>> for i in my_gen:
... pass
Hallo iter 1
>>> for i in my_gen:
... pass
正如@napuzba 指出的那样,__iter__
returns 每次使用时都是一个全新的生成器。相反,将状态存储在 self
:
class gen():
def __init__(self):
self.count = 0
def __iter__(self):
while self.count < 20:
self.count += 1
yield self.count