[Python-ideas] Debugging: some problems and possible solutions

Jonathan Fine jfine2358 at gmail.com
Wed Oct 3 11:15:22 EDT 2018


TITLE: Saving and sharing debug code
PROBLEM:
Sometimes debug-only code should be saved and shared. For example, one
person's code written to solve a bug might be needed a second time, by
someone else. Or perhaps the same person, again. This is particularly
likely when the bug is difficult or stubborn.

POSSIBLE SOLUTION
At present, __debug__ is a keyword identifier, which can take only the
values True and False. It gets its value on Python startup, and then
its value can't be changed.

The possible solution is to allow Python startup to give __debug__ an
additional value, namely 2. Once this is done, code blocks such as
    if __debug__ >= 2:
        # my debug-only code goes here
will effectively be ignored, except when requested.

Python already does this for blocks such as
    if __debug__:
        # ignored if optimised code is being generated

See also: https://docs.python.org/3/library/constants.html#__debug__
    __debug__
    This constant is true if Python was not started with an -O option.
See also the assert statement.


More information about the Python-ideas mailing list