Search code examples
pythonkeywordpython-internals

Why is __debug__ not in the keywords list when it's a keyword?


__debug__ is a built-in constant, which, according to the docs on constants, doesn't seem like an implementation detail.

When trying to assign to it (which isn't supported as the docs state) the following error message is displayed:

>>> __debug__ = False
  File "<stdin>", line 1
SyntaxError: assignment to keyword

Apparently, according to the message, it's a keyword, but:

>>> from keyword import kwlist
>>> '__debug__' in kwlist
False

Which seems odd. None, True and False, also listed as constants, are contained in the kwlist.

Why isn't __debug__ in the keyword list?


Solution

  • It's technically not a keyword in the sense of the Python grammar. There's a specific check in the compiler that prevents assignment to __debug__, but as far as the grammar is concerned, __debug__ isn't a keyword, and keyword.kwlist is generated from the grammar.