[Python-Dev] Assigning to __debug__

Guido van Rossum guido@digicool.com
Fri, 30 Mar 2001 12:19:31 -0500


>     GvR> The *only* acceptable use for __debug__ is to get rid of code
>     GvR> that is essentially an assertion but can't be spelled with
>     GvR> just an assertion, e.g.
> 
> Interestingly enough, last night Jim Fulton and I talked about a
> situation where you might want asserts to survive running under -O,
> because you want to take advantage of other optimizations, but you
> still want to assert certain invariants in your code.
> 
> Of course, you can do this now by just not using the assert
> statement.  So that's what we're doing, and for giggles we're multiply
> inheriting the exception we raise from AssertionError and our own
> exception.  What I think we'd prefer is a separate switch to control
> optimization and the disabling of assert.

That's one of the things I was alluding to when I talked about more
diversified control over optimizations.  I guess then the __debug__
variable would indicate whether or not assertions are turned on;
something else would let you query the compiler's optimization level.
But assigning to __debug__ still wouldn't do what you wanted (unless
we decided to *make* this the way to turn assertions on or off in a
module -- but since this is a compile-time thing, it would require
that the rhs of the assignment was a constant).

--Guido van Rossum (home page: http://www.python.org/~guido/)