Explaining Python Builtin Constants
Markdown--- name: explaining-python-builtin-constants description: Explains Python's built-in constants (True, False, None, NotImplemented, Ellipsis, __debug__) and site-module-added constants (quit, exit, copyright, credits, license). Use when clarifying constant behavior, differences between them, or interactive-shell-only availability. ---
Python>>> None is None True >>> NotImplemented # used by rich comparison/binary ops, not general code NotImplemented >>> ... is Ellipsis # `...` literal is Ellipsis True >>> __debug__ # False when run with -O True >>> exit() # only in interactive interpreter / site module enabled
-
Identify which category the constant belongs to:
- Core language constants (always available, part of
builtins):True,False,None,NotImplemented,Ellipsis/...,__debug__ - Site-module constants (added by
site.py, interactive convenience only):quit,exit,copyright,credits,license
- Core language constants (always available, part of
-
Explain semantics accurately:
True/False: instances ofbool, subclass ofint(True == 1,False == 0)None: sole instance ofNoneType, represents absence of a valueNotImplemented: return value from rich comparison/arithmetic special methods signaling "try the other operand's method"; not the same as raisingNotImplementedErrorEllipsis: singleton, written as..., used in slicing, type stubs, stub bodies__debug__:Trueunless Python started with-O/-OO; guardsassertstatementsquit/exit: callables (instances of_sitebuiltins.Quitter) that raiseSystemExit; only guaranteed in interactive mode — absent ifsitemodule is disabled (-Sflag) or in some embedded contextscopyright/credits/license: callables that print interpreter info when called (e.g.license()), added bysitemodule
-
Flag common confusions:
NotImplementedvsNotImplementedError(data value vs exception class)exit()/quit()are not meant for use in production scripts — usesys.exit()instead- These site-module constants may not exist if
site.pywasn't imported (python -S)
Example 1:
Input: "What's the difference between NotImplemented and NotImplementedError?"
Output:
NotImplemented is a singleton value returned from methods like __eq__, __lt__, __add__ to tell Python "I don't know how to handle this operand type, try the reflected method or fall back." Python then either calls the other object's method or raises TypeError itself.
NotImplementedError is an exception class, typically raised in abstract base methods to signal a subclass must override them.
Returning NotImplementedError() instead of NotImplemented from __eq__ is a common bug — it won't trigger Python's fallback protocol.
Example 2:
Input: "Why does exit() work in my REPL but fail in my script run with python -S script.py?"
Output:
exit and quit aren't language builtins — they're injected into the builtins namespace by the site module specifically for interactive convenience. The -S flag skips site initialization, so those names are never defined. For scripts, always use sys.exit([code]) from the sys module, which is guaranteed available.
Example 3:
Input: "Does assert cost anything in production?"
Output:
assert expr, msg compiles to a check guarded by __debug__. Running Python with -O sets __debug__ to False and the compiler strips assert statements entirely — no runtime cost. Don't rely on assert for input validation that must run in production; use explicit if/raise instead.
- Point users to
sys.exit()overexit()/quit()in any non-interactive code - Clarify that
True/False/Noneare keywords (cannot be reassigned) as of Python 3, unlikeNotImplemented/Ellipsiswhich are ordinary names bound to singletons - Mention
...as a valid no-op placeholder in function bodies and type stubs, equivalent topassbut semantically "not implemented yet" - When discussing
__debug__, tie it to the-O/-OOflags andPYTHONOPTIMIZEenv var
- Don't claim
quit/exit/copyright/credits/licenseare guaranteed builtins — they depend on thesitemodule being loaded - Don't confuse
NotImplemented(value) with raisingNotImplementedError(exception) - Don't say
assertis "always safe to use for validation" — it disappears under-O - Don't treat
Ellipsisas Python-2-only trivia — it's actively used in NumPy slicing and type stub (.pyi) syntax