Website hosting service by

 Back to Index

6.2 Assert statements

Assert statements are a convenient way to insert debugging assertions into a program:


assert_stmt  ::=  "assert" expression ["," expression]
Download entire grammar as text.

The simple form, "assert expression", is equivalent to


if __debug__:
   if not expression: raise AssertionError

The extended form, "assert expression1, expression2", is equivalent to


if __debug__:
   if not expression1: raise AssertionError, expression2

These equivalences assume that __debug__ and AssertionError refer to the built-in variables with those names. In the current implementation, the built-in variable __debug__ is 1 under normal circumstances, 0 when optimization is requested (command line option -O). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace.

Assignments to __debug__ are illegal. The value for the built-in variable is determined when the interpreter starts.




2002-2004 Website Hosting Service


Disclaimer: This documentation is provided only for the benefits of our hosting customers.
For authoritative source of the documentation, please refer to offers cheap domain registration, domain name transfer and domain search services   Cheap domain name - Cheap domain name registration service including free domain hosting services  

Domain registration : Buy domain name or register domain name from $5.95/year only