Python design: why is a statement approved, not a function?

In Python, assert is an instruction, not a function. Was this a deliberate decision? Are there any advantages to assert being an expression (and a reserved word) instead of a function?

According to docs , assert expression1, expression2 expands to

 if __debug__: if not expression1: raise AssertionError(expression2) 

The docs also say that "the current code generator does not generate code for the assert statement when requesting optimization at compile time." Without knowing the details, it seems that in order to make this possible, a special case was required. But then a special case could also be used to optimize calls to assert() .

If assert was a function, you could write:

 assert(some_long_condition, "explanation") 

But since assert is an operator, the tuple always evaluates True and you get

 SyntaxWarning: assertion is always true, perhaps remove parentheses? 

The correct way to write:

 assert some_long_condition, \ "explanation" 

which is perhaps less cute.

+44
python assert language-design
Nov 15 '12 at 1:45
source share
4 answers

Are there any advantages to assert (and reserved word) assertion instead of function?

  • It is not possible to reassign a user function, which means that it can be effectively disabled at compile time, as @mgilson pointed out.
  • The evaluation of the second optional parameter is deferred until // if the statement fails. It is embarrassing to do this using functions and function arguments (you would have to skip the lambda.) Without delaying the evaluation of the second parameter, you enter additional overhead.
+28
Nov 15 '12 at 2:20
source share
— -

One of the great features of assert in python and other languages ​​(particularly C) is that you can remove them to optimize your code by simply adding the correct #define (not necessarily on the command line with any compiler I've ever used) or optimization flags ( -O in python). If the assert function became a function, this function could not be added in python, since you do not know before it is executed whether you have a built-in function assert or a user-defined function with the same name.




Also note that in python function calls are quite expensive. Replacing if __debug__: ... inline code if __debug__: ... is probably much more efficient than calling a function, which can be significant if you put the assert in a performance-critical procedure.

+16
Nov 15 '12 at 2:16
source share

I'm not an expert in Python, but I think performance is one of the biggest reasons.

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

Extending assert, expression and explanation are not actually evaluated if they are not really needed (when debugging is evaluated as true). I believe that it is very important if we want the statement to not affect performance when it was not necessary (i.e. Performance was not produced in the production system).

+6
Nov 15
source share

In addition to the other answers (and seemingly off-topic) review. To avoid using backslashes, you can use implicit line joins inside brackets .; -)

Instead:

 assert some_long_condition, \ "explanation" 

You can write:

 assert some_long_condition, ( "explanation") 
+5
01 Oct '13 at 6:54 on
source share



All Articles