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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With