-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathiter_protocols.py
48 lines (34 loc) · 1.01 KB
/
iter_protocols.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def iter_example():
for x in y:
... # loop body
it = iter(y) # y.__iter__()
x = next(it) # it.__next__()
... # loop body
x = next(it)
... # loop body
x = next(it) # eventually raises StopIteration
async def aiter_example():
async for x in y:
... # loop body
it = aiter(y) # y.__aiter__()
x = await anext(it) # it.__anext__()
... # loop body
x = await anext(it)
... # loop body
x = await anext(it) # eventually raises StopAsyncIteration
class Iterable:
def __iter__(self):
return ... # return some iterator
class Iterator:
def __next__(self):
return ... # get the next element or raise StopIteration
def __iter__(self):
return self
class AsyncIterable:
def __aiter__(self):
return ... # return some async iterator
class AsyncIterator:
async def __anext__(self):
return ... # get the next element or raise StopAsyncIteration
def __aiter__(self):
return self