Why assert is not a function?

Barry barry at barrys-emacs.org
Tue Mar 2 17:25:44 EST 2021



> On 2 Mar 2021, at 20:54, Marco Sulla <Marco.Sulla.Python at gmail.com> wrote:
> 
> I have a curiosity. Python, as many languages, has assert as a
> keyword. Can't it be implemented as a function? Is there an advantage
> to have it as a keyword?

assert condition, expression

Only is condition is false with expression be evaluated.
So you can safely do expensive things I the expression with incuring and cost if the condition is True.

With a function assert the 2nd part would have to evaluated regardless of the state of the condition.
Which would slow down the code for no benefit.

Barry

> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list