在 python 中屏蔽(无效)断言

shield (invalidate) assert in python

当我 运行 作为发行版时,我想使 Python 代码中的 "assert" 无效。考虑到我的文件在开发过程中可能会用到很多"assert",但是为了提高效率,我想跳过"assert"的编译。在 C++ 中是否有一些简单的方法,例如 pre_define of "NDEBUG"?

使用命令行选项-O。如the docs所述:

In the current implementation, the built-in variable __debug__ is True under normal circumstances, False when optimization is requested (command line option -O). The current code generator emits no code for an assert statement when optimization is requested at compile time.