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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.