idiom for debug code?

Dan Perl danperl at rogers.com
Thu Sep 30 15:56:44 EDT 2004


Thanks.  I'll have to look more into the consequences of being dependent on 
the use of the "-O" option, but it might be useful.  I'm wondering about 
"-O" because then it all depends on how you are running the script.  I 
usually have a
    #!/usr/bin/env python
at the beginning of the top-level script and I run it without explicitly 
invoking 'python'.  Also, I haven't used py2exe yet and I wonder how "-O" 
would fit with that.  Like I said, I would have to look into it more, but it 
is an interesting idea.

Why are assignments to __debug__ illegal?  I tried to assign to it and it 
raised a SyntaxError exception, so it is enforced.  But anyone knows what is 
the reason behind this design choice?

I've done a search on that and I found a few interesting things, but still 
no reason.  BTW, here are some of those interesting things, basically 
matters of history:

"After a public outcry, assignment to __debug__ is no longer illegal; 
instead, a warning is issued. It will become illegal in 2.2." 
(http://www.python.org/2.1/NEWS.txt)

"The assert statement no longer checks the __debug__ flag, so you can no 
longer disable assertions by assigning to __debug__. Running Python with 
the -O switch will still generate code that doesn't execute any assertions." 
(http://www.python.org/doc/2.3.4/whatsnew/node17.html)

Looking back on the documentation for assert, I see that it actually says 
"is equivalent to", so there is no conflict between the two documents.  But 
that raises the question: what is __debug__ meant for now?

And what does "-O" do besides disabling assertions?  I don't see any 
difference in performance when running my script with or without "-O".

Dan

"Robert Brewer" <fumanchu at amor.org> wrote in message 
news:mailman.4149.1096566273.5135.python-list at python.org...
Dan Perl wrote:
> Is there a mechanism or an idiom for adding code for
> debugging so that it
> can easily be removed in the production code?  I am thinking
> of something
> similar to the C/C++ preprocessor statements with which you
> can compile an
> application with the debug code or without it (the default).

>From the docs (http://docs.python.org/ref/assert.html):

----

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.

----

...which implies other uses for __debug__, I think, which might fit your
needs.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org 





More information about the Python-list mailing list