Skip to content
Advertisement

Semantic equivalent of async for

From the docs relative to async for syntax in Python 3.5, I gathered that it was introduced to iterate over an awaitable iterator.

There is something I don’t get in the semantic equivalent that follow the description though:

JavaScript

What is the line iter = type(iter).__aiter__(iter) doing? Why is it necessary?

Advertisement

Answer

Magic methods in python, e.g. __add__, are always looked up on the class. For example, len(foo) will actually call resolve to type(foo).__len__(foo). This means it will never use a foo.__len__ if __len__ is defined on foo.

Since __aiter__ is a magic method, it is thus always looked up as type(foo).__aiter__(foo).


Example for lookup:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement