HeadlinesBriefing favicon HeadlinesBriefing.com

Python's Pre-Declared Constants Behavior Explained

Hacker News •
×

Python has six pre-declared constants: True, False, None, __debug__, Ellipsis (or ...), and Not Implemented. Each behaves differently. True, False, and None are keywords — lexical tokens rather than identifiers — causing expressions like `x. True` to raise a Syntax Error. __debug__ is a boolean constant normally True, but False with the `-O` flag.

It cannot be assigned to or deleted, raising Syntax Error for both, yet `x.__debug__` raises Attribute Error instead. Assigning via `setattr(builtins, '__debug__', 67)` works but doesn't affect the lexical token's value. Ellipsis and Not Implemented are normal builtins, not keywords, so they can be shadowed by globals. Interestingly, `setattr(builtins, 'True', 67)` changes the builtin but not the keyword's value.

The article questions the rationale behind these inconsistencies and notes that Syntax Error is raised in cases like assigning to `__debug__` or using `yield`/`await` outside functions, despite valid syntax.