__debug__ http://stackoverflow.com/questions/15305688
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Nov 17 02:35:26 EST 2016
On Thursday 17 November 2016 02:22, eryk sun wrote:
> On Wed, Nov 16, 2016 at 8:39 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> On Wednesday 16 November 2016 16:21, Veek M wrote:
>>
>>> 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'
>>
>> What he means is, "I didn't test this code before running it, and I am
>> wrong."
>>
>> You cannot rebind __debug__.
>>
>>>>> __debug__ = False
>> File "<stdin>", line 1
>> SyntaxError: can not assign to __debug__
>>
>>
>> (That's Python 2.5 or better, and maybe even older than that.)
>
> Andrew didn't assign directly to __debug__. He assigned to
> sys.modules[__name__].__debug__, which is allowed prior to 2.7. Even
> in later versions, as he pointed out, you can dynamically assign to
> '__debug__' in a namespace dict.
That's a fascinating loophole.
In any case, that's not something people are likely to stumble onto by
accident, and if you're intentionally writing to a reserved name in a non-
standard way, whatever side-effects you trip over are your own fault.
> >>> if not __debug__: print('not __debug__')
> ...
> not __debug__
That should be a candidate for keyhole optimization, same as `if __debug__`.
--
Steven
299792.458 km/s — not just a good idea, it’s the law!
More information about the Python-list
mailing list