[Python-Dev] PEP 492: async/await in Python; version 4

Yury Selivanov yselivanov.ml at gmail.com
Fri May 1 22:06:27 CEST 2015


On 2015-05-01 3:23 PM, Yury Selivanov wrote:
> Let's say it this way: I want to know what I am looking at
> when I browse through the code -- an asynchronous iterator,
> or a normal iterator.  I want an explicit difference between
> these protocols, because they are different.
>
> Moreover, the below code is a perfectly valid, infinite
> iterable:
>
>     class SomeIterable:
>          def __iter__(self):
>              return self
>          async def __next__(self):
>              return 'spam'
>
> I'm strong -1 on this idea.
>

To further clarify on the example:

     class SomeIterable:
          def __iter__(self):
              return self
          async def __aiter__(self):
              return self
          async def __next__(self):
              print('hello')
              raise StopAsyncIteration


If you pass this to 'async for' you will get
'hello' printed and the loop will be over.

If you pass this to 'for', you will get an
infinite loop, because '__next__' will return a
coroutine object (that has to be also awaited,
but it wouldn't, because it's a plain 'for'
statement).

This is something that we shouldn't let happen.

Yury


More information about the Python-Dev mailing list