How to disable assert statements inside a module?

Tim Peters tim.one at comcast.net
Sun Mar 10 16:17:01 EST 2002


[Pearu Peterson]
> Is there a way to disable all assert statements inside a module,
> and without calling python with -O switch?

[Martin v. Loewis]
> You can set __debug__ to 0 on the module level; this will disable
> assert statements inside this module.

Note that assignment to __debug__ was deprecated in 2.1:

    SyntaxWarning: can not assign to __debug__

and removed in 2.2:

    SyntaxError: can not assign to __debug__

-O is the only way left.  Guido often writes code under the control of an
explicit "debug" vrbl instead.  A more flexible way to control asserts would
be (IMO) prime PEP material (as a heavy assert user myself, I sympathize
with Pearu's dilemma -- "all or nothing everywhere" is too crude for large
projects with many subsystems).





More information about the Python-list mailing list