[Python-ideas] Repurpose `assert' into a general-purpose check

M.-A. Lemburg mal at egenix.com
Tue Nov 28 03:29:08 EST 2017

On 28.11.2017 03:28, Ivan Pozdeev via Python-ideas wrote:
> On 28.11.2017 5:19, Chris Angelico wrote:
>> Actually, Python does have a way of disabling assertions (the -O
>> flag), so they should be treated the same way they are in C.
>> Assertions should not be used as shorthands for "if cond: raise Exc"
>> in the general case.
> I'm claiming, and provided evidence, that there are no use cases for
> this in Python, so no-one (of any significance) will suffer when the
> disabling is cut out.

... except those who have relied on this behavior for around
two decades to make their code run faster in production
environments and those who run on memory constrained systems
using -OO to remove doc-strings as well.

asserts are meant as debug tool and runtime way to document
application internal expectations of code following them.
Once all tests pass they are no longer needed.

If you find that you still need them, you should recode the
asserts as proper if statements.

As example use case, asserts testing user provided data are
not a good idea, since you cannot test all possible user
inputs. For those cases, an if statement is the right way
to implement your checks.

Marc-Andre Lemburg

Professional Python Services directly from the Experts (#1, Nov 28 2017)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the Python-ideas mailing list