[Python-Dev] Assigning to __debug__

M.-A. Lemburg mal@lemburg.com
Fri, 30 Mar 2001 17:10:17 +0200


Guido van Rossum wrote:
> 
> > So you are suggesting that we change all our code to something like:
> >
> > __enable_debug__ = 0 # set to 0 for production mode
> >
> > ...
> >
> > if __debug__ and __enable_debug__:
> >    print 'debugging information'
> >
> > ...
> 
> I can't suggest anything, because I have no idea what semantics you
> are assuming for __debug__ here, and I have no idea what you want with
> that code.  Maybe you'll want to say "__debug__ = 1" even when you are
> in -O mode -- that will definitely not work!

I know, but that's what I'm expecting. The point was to be able
to disable debugging code when running Python in non-optimized mode.
We'd have to change our code and use a new variable to work
around the SyntaxError exception.

While this is not so much of a problem for new code, existing code
will break (ie. not byte-compile anymore) in Python 2.1. 

A warning would be OK, but adding yet another SyntaxError for previously 
perfectly valid code is not going to make the Python users out there 
very happy... the current situation with two different settings
in common use out there (Python 1.5.2 and 2.0) is already a pain
to maintain due to the issues on Windows platforms (due to DLL 
problems).

I don't think that introducing even more subtle problems in 2.1
is going to be well accepted by Joe User.
 
> The form above won't (currently) be optimized out -- only "if
> __debug__:" is optimized away, nothing more complicated (not even "if
> (__debug__):".

Ok, make the code look like this then:

if __debug__:
   if enable_debug:
       print 'debug info'
 
> In any case, YOU SHOULD NEVER INTRODUCE VARIABLES USING THE
> __UNDERSCORE__ CONVENTION!  Those names are reserved for the
> interpreter, and you risk that they will be assigned a different
> semantics in the future.

Hey, this was just an example... ;-)

> > I don't see the point in having to introduce a new variable
> > just to disable debugging code in Python code which does not
> > run under -O.
> >
> > What does defining __debug__ as read-only variable buy us
> > in the long term ?
> 
> It allows the compiler to assume that __debug__ is a built-in name.
> In the future, the __debug__ variable may become meaningless, as we
> develop more differentiated optimization options.
> 
> The *only* acceptable use for __debug__ is to get rid of code that is
> essentially an assertion but can't be spelled with just an assertion,
> e.g.
> 
> def f(L):
>     if __debug__:
>         # Assert L is a list of integers:
>         for item in L:
>             assert isinstance(item, type(1))
>     ...

Maybe just me, but I use __debug__ a lot to do extra logging or 
printing in my code too; not just for assertions.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Pages:                           http://www.lemburg.com/python/