Remember
aiter() obtains an asynchronous iterator from an asynchronous iterable. Retrieve its values with asynchronous iteration.
aiter() obtains an asynchronous iterator from an asynchronous iterable. Retrieve its values with asynchronous iteration.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
import asyncio
async def items():
for number in range(3):
yield number
async def main():
iterator = aiter(items())
print(await anext(iterator, "done"))
asyncio.run(main())Top-level scope
function itemsObject 2 · functionfunction mainaiter() obtains an asynchronous iterator from an asynchronous iterable. Retrieve its values with asynchronous iteration.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
import asyncio
async def items():
for number in range(3):
yield number
async def main():
iterator = aiter(items())
print(await anext(iterator, "done"))
asyncio.run(main())
|
v
DISPLAYED RESULT
0
FINAL BINDINGS
items --> object 1 (function)
main --> object 2 (function)
object 1: function items
object 2: function mainChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
import asyncio
async def items():
for number in range(3):
yield number
async def main():
iterator = aiter(items())
print(await anext(iterator, "done"))
asyncio.run(main())Top-level scope
function itemsObject 2 · functionfunction mainThe example has finished. Compare the final output with the values and references above.
0
What do you expect this program to print?
import asyncio
async def items():
for number in range(3):
yield number
async def main():
iterator = aiter(items())
print(await anext(iterator, "done"))
asyncio.run(main())aiter() FunctionThe aiter() function in Python is used to get an asynchronous iterator from an asynchronous iterable. It is especially useful when working with async for loops and asynchronous generators.
aiter(async_iterable)
async_iterable – An object that supports asynchronous iteration (must implement __aiter__()).async for loop.import asyncio
class AsyncCounter:
def __init__(self, limit):
self.limit = limit
def __aiter__(self):
self.count = 0
return self
async def __anext__(self):
if self.count < self.limit:
self.count += 1
return self.count
else:
raise StopAsyncIteration
async def main():
async_iter = aiter(AsyncCounter(3))
async for num in async_iter:
print(num)
asyncio.run(main())
1
2
3
aiter() on a regular iterable like a list will raise a TypeError.__aiter__() and __anext__().If you’re working on async I/O or streaming data, aiter() can be used to create pipelines that process data asynchronously.
aiter() returns an asynchronous iterator.async for loops.__aiter__() and __anext__().Create an asynchronous class that yields even numbers from 2 to 10 using aiter().
Connect what you noticed in the example with the idea below.
aiter() obtains an asynchronous iterator from an asynchronous iterable. Retrieve its values with asynchronous iteration.
aiter() turns every ordinary list into an asynchronous iterator.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.