design of python: why is assert a statement and not a function?

前端 未结 4 658
滥情空心
滥情空心 2020-12-13 12:26

In Python, assert is a statement, and not a function. Was this a deliberate decision? Are there any advantages to having assert be a statem

4条回答
  •  一生所求
    2020-12-13 12:55

    I am no expert in Python, but I believe performance is one of the biggest reason.

    if we have assert(expression, explanation) as function, if expression is expensive to evaluate, even we are in non-debug mode, Python needs to evaluate both expression to pass it to the assert function.

    By expanding the assert, the expression and explanation statement is in fact not evaluated unless they are really needed (when debug evaluates to true). I believe it is critical if we want to make assert not affecting performance when not necessary (i.e. no performance hit in production system).

提交回复
热议问题