1

In C/C++, a programmer can easily enable/disable assert statements with macros in the source code. Can this be done in a similar way in python?

I know assertions can be disabled using the -O flag (capital O) in python. However, I prefer to do this in the source code.

I am using python 3.7.3 and Windows 10.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295

1 Answers1

2

I think there is no comparable way to achieve this in Python. The -O flag sets the built-in variable __debug__ to False, but Python does not allow for changing it at run-time.

One possible solution would be to encapsulate your assertions in if-statements, using a global variable to control whether assert statements get executed or not, but I doubt this is the answer you're looking for.

For more information about the topic, you might want to look at this answer to a related question.

tcdude
  • 91
  • 1
  • 2