ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH
advanced | search help

Reference
ActivePython 2.4
Python Documentation
Language Reference
6. Simple statements
6.1 Expression statements
6.2 Assert statements
6.3 Assignment statements
6.4 The pass statement
6.5 The del statement
6.6 The print statement
6.7 The return statement
6.8 The yield statement
6.9 The raise statement
6.10 The break statement
6.11 The continue statement
6.12 The import statement
6.13 The global statement
6.14 The exec statement

MyASPN >> Reference >> ActivePython 2.4 >> Python Documentation >> Language Reference >> 6. Simple statements
ActivePython 2.4 documentation


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 True under normal circumstances, False 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.

See About this document... for information on suggesting changes.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved