Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is async in Python?

I've read about new Python "keywords" async and await. However they are neither really keywords nor reserved in a namespace.

>>> import keyword
>>> keyword.iskeyword("async")
False
>>> async
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'async' is not defined

In the example I would expect True and SyntaxError for a keyword.

So, what exactly is async in Python? How it works?

like image 794
Thyrst' Avatar asked Apr 07 '17 21:04

Thyrst'


1 Answers

For backward compatibility purposes, in Python 3.5 and 3.6, async and await are parsed through an ugly tokenizer hack. Inside an async def function definition, or for an async directly before a def, the tokenizer replaces NAME tokens for async and await with ASYNC and AWAIT tokens; in other contexts, the tokenizer emits regular NAME tokens for async and await, treating them as identifiers.

You can see the code that handles it in Parser/tokenizer.c, and you can find the backward compatibility plan in PEP 492, the PEP that introduced async and await syntax.

like image 99
user2357112 supports Monica Avatar answered Nov 04 '22 08:11

user2357112 supports Monica