Why assert is not a function?

Chris Angelico rosuav at gmail.com
Wed Mar 3 09:54:18 EST 2021


On Thu, Mar 4, 2021 at 1:40 AM Grant Edwards <grant.b.edwards at gmail.com> wrote:
>
> On 2021-03-02, Chris Angelico <rosuav at gmail.com> wrote:
> > On Wed, Mar 3, 2021 at 10:22 AM Mirko via Python-list><python-list at python.org> wrote:
> >
> >> In production code you don't want any asserts, but logging. Having
> >> "assert" being a function would make it much harder to get rid of
> >> it in production code.
> >
> > Really?
> >
> > if PRODUCTION:
> >     def assert(*a, **kw): pass
> >
> > would work if it were a function :)
>
> Wouldn't that still evaluate all of the arguments? You get rid of the
> value of the assert, but retain almost all of the cost.
>
> I thought the entire point of asser being a keyword was so that if you
> disable asserts then they go away completely: the arguments aren't
> even evaluated.
>

It depends on what the point of "removing the assertions" is, but yes,
that will indeed still evaluate the arguments. IMO the cost of running
assertions isn't that high compared to the value of keeping them
(which is why I never run -O), and the performance argument is a weak
one compared to the much stronger value of having the actual failing
expression available in the exception report.

ChrisA


More information about the Python-list mailing list