Disabling python's assert() without -0 flag

前端 未结 3 2221
悲哀的现实
悲哀的现实 2020-12-15 09:26

I\'m running a python script from inside a different software (it provides a python interface to manipulate its data structures).

I\'m optimizing my code for speed

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 09:51

    As @unutbu describes, there is no official way of doing this. However, a simple strategy is to define a flag like _test somewhere (for example, as keyword argument to a function, or as a global variable in a module), then include this in your assert statements as follows:

    def f(x, _test=True):
        assert not _test or x > 0
        ...
    

    Then you can disable asserts in that function if needed.

    f(x, _test=False)
    

提交回复
热议问题