Why assert is not a function?

Chris Angelico rosuav at gmail.com
Wed Mar 3 13:41:02 EST 2021


On Thu, Mar 4, 2021 at 5:25 AM Barry Scott <barry at barrys-emacs.org> wrote:
>
>
>
> > On 3 Mar 2021, at 17:35, David Lowry-Duda <david at lowryduda.com> wrote:
> >
> >> 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.
> >
> > I think I've only every used a string as the expression. Have you found
> > it useful to use complicated, expensive expressions? Could you give an
> > example?
>
> In the test suite of the product I work on its common to put a lot of information into the expression.
> For example if a HTTP request returns unexpected content we will include all the headers and
> body of the that the request returners.
>
>         assert http_status == '200', 'Request failed status %r Body:\n%s' % (http_status, http_header_and_body)
>

That doesn't look like the sort of thing that should be an assertion.
Assertions should be for things that, if your code were bug-free,
could never happen.

ChrisA


More information about the Python-list mailing list