Built-in Constants
__debug__ — compile-time debug flag
__debug__ is a Boolean constant indicating whether the code was compiled without optimization. In normal interpreter execution it is True; -O and -OO select optimized compilation, where it is False. Assertions are omitted at positive optimization levels, including their condition and message expressions. A conditional guarded by __debug__ can therefore be used for optional diagnostics, not required validation, authorization or essential side effects. The flag is not a configurable logger level and cannot be assigned in Python source. Explicit compile() optimization settings can differ from the process default; the flag reflects the compiled code.
Formal Syntax & Structure
python syntaxpython
__debug__Return Value
bool
True for non-optimized code; False for optimized code.
Working Example
Example Snippetpython
def validate(quantity): if quantity < 0: raise ValueError("negative quantity") if __debug__: print("debug: checked") return quantity
print(validate(3))try: validate(-1)except ValueError: print("rejected")