← All stories
● Covered by 1 source · 1 reportLow impact1 neutral

Python's Pre-Declared Constants Exhibit Inconsistent Behaviors

🔄 Updated 2h ago
New to BrevFeed? We gather this story from every outlet covering it into one summary — ranked by real-world impact, not just the latest headline — so you never miss what matters. What is BrevFeed? →

Key points

  • True, False, and None are keywords, parsed lexically.
  • __debug__ is an identifier but cannot be assigned to.
  • Error types for constant manipulation vary unexpectedly.
  • These constants behave differently from other Python elements.

Variations in Python's Core Constants

Python includes six pre-declared constants: True, False, None, __debug__, Ellipsis, and NotImplemented. Despite their common classification as constants, their internal mechanisms and behaviors differ significantly. These inconsistencies are evident in how they are parsed, whether they can be reassigned, and the types of errors generated when attempting to modify them.

Keyword Constants: True, False, and None

True, False, and None are unique in Python as they are keywords, not identifiers. This means they are resolved during the lexical analysis phase, rather than through standard name resolution. A consequence of this design is that expressions like x.True result in a SyntaxError, as the lexer interprets 'True' as a keyword in an invalid context.

__debug__: A Special Identifier

The __debug__ constant is a boolean, typically True, but becomes False when Python is run with the -O (optimization) flag. Unlike True, False, and None, __debug__ is an identifier. However, it is unique among identifiers because it cannot be assigned to, either directly (e.g., __debug__ = 67) or as an attribute (e.g., x.__debug__ = 67), both of which raise a SyntaxError. Attempts to delete __debug__ also result in a SyntaxError, though deleting an attribute named __debug__ on an object would raise an AttributeError or NameError, not a SyntaxError.

Inconsistent Error Handling

The error types generated when interacting with these constants are not uniform. While assigning to __debug__ raises a SyntaxError, attempting to access x.__debug__ when x is undefined or lacks the attribute would result in an AttributeError, as it is syntactically valid. This divergence in error reporting for seemingly related operations highlights the distinct internal handling of these pre-declared constants within the Python language.

✨ This summary was generated by AI from the outlets' reporting listed below. It is not independently verified and may contain errors — check the original sources. How BrevFeed works →

The daily brief

One email each morning: the day's tech stories, clustered across outlets and summarized. No account needed.

One email a day. Unsubscribe in one click, any time.

Today's brief

Spend a few minutes, get the whole day. Every topic's top stories in one hands-free rundown — listen, watch, or read the transcript.

~12 min · 12 stories · Aug 25

▶ Play today's brief Listen on Spotify

New every morning, and the back catalogue is archived by date.

Reporting from

Python's six pre-declared constants (True, False, None, __debug__, Ellipsis, NotImplemented) demonstrate varied and sometimes unexpected behaviors regarding their lexical parsing, mutability, and error handling. This inconsistency highlights unique design choices within the language's core, affecting how developers interact with these fundamental elements.