Robert
Robert

Reputation: 525

Turn custom debugging functions on/off in Python

If I define my own debugging module with functions that should only get executed while developing, is there a way to completely disable those functions when I'm ready to release the final version?

I was hoping there was a clever way to have the environment completely skip over the function calls during the byte-code conversion? I searched for this, but may be using the wrong search inputs.

I'm developing an add-on for Blender, so I don't believe I have any control over the compilation or conversion.

Upvotes: 1

Views: 657

Answers (1)

Migwell
Migwell

Reputation: 20169

Yes, Python has a -O flag, which means:

Remove assert statements and any code conditional on the value of __debug__

So basically if you write your debug code using either assert statements, or you check the value of __debug__ before running your debugging functions, you can use -O to switch on a production mode:

if __debug__:
    run_debug_function()

You can also enable optimization by setting the PYTHONOPTIMIZE environment variable to some non-empty string, e.g. export PYTHONOPTIMIZE=1 in your shell.

For more info refer to the Python command line documentation

Upvotes: 3

Related Questions