william007
william007

Reputation: 18547

is it possible to let a piece of code to run only during debug?

Does python provide functionality that only allow a piece of code to run during debug, a bit like preprocessor in c++?

Upvotes: 0

Views: 90

Answers (2)

tamasgal
tamasgal

Reputation: 26329

I would suggest using the logger module (which is always great) and check for the log-level.

https://docs.python.org/2/library/logging.html

For example by using the getEffectiveLevel():

https://docs.python.org/2/library/logging.html#logging.Logger.getEffectiveLevel

This way you have some other nifty features like log messages with different levels…

Upvotes: 0

mgilson
mgilson

Reputation: 310227

Python has a builtin constant __debug__ that can be used for this purpose.

python -O something.py  # __debug__ == False
python something.py  #  __debug__ == True

Note that there is also the assert statement which can be optimized out completely. . .

Upvotes: 2

Related Questions