__debug__ http://stackoverflow.com/questions/15305688/conditional-debug-statement-not-executed-though-debug-is-true

dieter dieter at handshake.de
Wed Nov 16 03:01:33 EST 2016


Veek M <vek.m1234 at gmail.com> writes:

> Trying to make sense of that article. My understanding of debug was 
> simple:
> 1. __debug__ is always True, unless -O or -OO
> 2. 'if' is optimized out when True and the expr is inlined.
>
> So what does he mean by:
>
> 1. 'If you rebind __debug__, it can cause symptoms'
> 2. 'During module compilation, the same code that handles literals also 
> handles the magic constants ..., None, True, False, and __debug__'
> 3. 'you'll see that if __debug__: statements are either removed 
> entirely, or use LOAD_CONST to load the compile-time debug constant, 
> while if bool(__debug__): statements use LOAD_GLOBAL to load the value 
> of __debug__.'
>
> 4. 'Of course these are guaranteed to be the same… unless you rebind 
> __debug__'
>
> Basically every line in that answer is new to me..

It essentially tells you:
 
  "__debug__" has an internal use; only read it; never write (rebind) it.

The rest are the details explaining what can go wrong when you
write (rebind) "__debug__" and why.

The "why" comes essentially from the fact that in some (but not all)
cases "__debug__" is handled at compile time, while a potential
writing it handled at runtime. Thus, after you have changed "__debug__",
things may not work as you expect.


If you keep in mind "do not change the value of __debug__ at runtime", you
can forget all the details.




More information about the Python-list mailing list