Why assert is not a function?

Chris Angelico rosuav at gmail.com
Tue Mar 2 18:50:43 EST 2021


On Wed, Mar 3, 2021 at 10:48 AM Hexamorph <hexamorph at gmx.net> wrote:
>
> Am 03.03.2021 um 00:24 schrieb Chris Angelico:
> > On Wed, Mar 3, 2021 at 10:22 AM Mirko via Python-list
> > <python-list at python.org> wrote:
> >>
> >> Am 02.03.2021 um 23:09 schrieb Stestagg:
> >>> Ignoring the question about this feature being particularly useful, it
> >>
> >> It is useful because "assert" is primarily (if not purely and
> >> exclusive) a debugging tool during development and testing.
> >>
> >> 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?
>
> Yes.
>
> > if PRODUCTION:
> >     def assert(*a, **kw): pass
> >
> > would work if it were a function :)
>
> With the "if PRODUCTION" being superfluously in the production code
> (not to mention the possible NameError otherwise).
>

Whatever method you have for determining whether you're in debug or
prod, you can use it to decide whether or not to create the function.
It's that easy.

That said, though... I prefer to keep my assertions in prod too.

ChrisA


More information about the Python-list mailing list