[英]python iterators, generators and in between
So I get generator functions for lazy evaluation and generator expressions, aka generator comprehensions as its syntactic sugar equivalent.所以我得到了用于惰性求值和生成器表达式的生成器函数,也就是生成器推导式作为它的语法糖等价物。
I understand classes like我理解类
class Itertest1:
def __init__(self):
self.count = 0
self.max_repeats = 100
def __iter__(self):
print("in __inter__()")
return self
def __next__(self):
if self.count >= self.max_repeats:
raise StopIteration
self.count += 1
print(self.count)
return self.count
as a way of implementing the iterator interface, ie iter () and next () in one and the same class.作为实现迭代器接口的一种方式,即iter ()和next ()在同一个类中。
But what then is但那是什么
class Itertest2:
def __init__(self):
self.data = list(range(100))
def __iter__(self):
print("in __inter__()")
for i, dp in enumerate(self.data):
print("idx:", i)
yield dp
which uses the yield statement within the iter member function?它在iter成员函数中使用了 yield 语句?
Also I noticed that upon calling the iter member function我还注意到在调用 iter 成员函数时
it = Itertest2().__iter__()
batch = it.__next__()
the print statement is only executed when calling next () for the first time. print语句只在第一次调用next ()时执行。 Is this due to this weird mixture of yield and iter?
这是由于收益率和迭代的这种奇怪的混合吗? I think this is quite counter intuitive...
我认为这很违反直觉......
Having the yield
statement anywhere in any function wraps the function code in a (native) generator object, and replaces the function with a stub that gives you said generator object.在任何函数中的任何位置都有
yield
语句将函数代码包装在(本机)生成器对象中,并用一个存根替换该函数,该存根为您提供所述生成器对象。
So, here, calling __iter__
will give you an anonymous generator object that executes the code you want.因此,在这里,调用
__iter__
将为您提供一个执行所需代码的匿名生成器对象。
The main use case for __next__
is to provide a way to write an iterator without relying on (native) generators. __next__
的主要用例是提供一种无需依赖(本机)生成器即可编写迭代器的方法。
The use case of __iter__
is to distinguish between an object and an iteration state over said object. __iter__
的用例是区分对象和该对象上的迭代状态。 Consider code like考虑像这样的代码
c = some_iterable()
for a in c:
for b in c:
# do something with a and b
You would not want the two interleaved iterations to interfere with each other's state.您不希望两个交错的迭代干扰彼此的状态。 This is why such a loop would desugar to something like
这就是为什么这样的循环会脱糖成类似的东西
c = some_iterable()
_iter1 = iter(c)
try:
while True:
a = next(_iter1)
_iter2 = iter(c)
try:
while True:
b = next(_iter2)
# do something with a and b
except StopIteration:
pass
except StopIteration:
pass
Typically, custom iterators implement a stub __iter__
that returns self
, so that iter(iter(x))
is equivalent to iter(x)
.通常,自定义迭代器实现一个返回
self
的存根__iter__
,因此iter(iter(x))
等价于iter(x)
。 This is important when writing iterator wrappers.这在编写迭代器包装器时很重要。
Something equivalent to Itertest2
could be written using a separate iterator class.可以使用单独的迭代器类编写与
Itertest2
等效的东西。
class Itertest3:
def __init__(self):
self.data = list(range(100))
def __iter__(self):
return Itertest3Iterator(self.data)
class Itertest3Iterator:
def __init__(self, data):
self.data = enumerate(data)
def __iter__(self):
return self
def __next__(self):
print("in __inter__()")
i, dp = next(self.state) # Let StopIteration exception propagate
print("idx:", i)
return dp
Compare this to Itertest1
, where the instance of Itertest1
itself carried the state of the iteration around in it.与此相比,
Itertest1
,其中的实例Itertest1
本身承载的迭代周围的状态。 Each call to Itertest1.__iter__
returned the same object (the instance of Itertest1
), so they couldn't iterate over the data independently.对
Itertest1.__iter__
每次调用Itertest1.__iter__
返回相同的对象( Itertest1
的实例),因此它们无法独立迭代数据。
Notice I put print("in __iter__()")
in __next__
, not __iter__
.请注意,我将
print("in __iter__()")
放在__next__
,而不是__iter__
。 As you observed, nothing in a generator function actually executes until the first call to __next__
.正如您所观察到的,在第一次调用
__next__
之前,生成器函数中的任何内容实际上都不会执行。 The generator function itself only creates an generator;生成器函数本身只创建一个生成器; it does not actually start executing the code in it.
它实际上并没有开始执行其中的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.