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

Petr Viktorin encukou at gmail.com
Wed Nov 29 17:43:33 EST 2017

On 11/29/2017 08:31 PM, Barry Warsaw wrote:
> Nathan Schneider wrote:
>> I think it would be interesting to investigate how assert statements are
>> used in the wild. I can think of three kinds of uses:
> assert statements are also a form of executable documentation.  It says
> something like "I think the internal state of my code must be this way,
> otherwise I don't really understand what I wrote".
> Now, maybe you could argue that such use should be enabled
> unconditionally, but I disagree since usually your understanding of the
> state of your code is correct, so the additional checks are unnecessary,
> and people *do* use -O and -OO in practice.  And these days it's even
> more practical to do so, given the `opt-*` level of pyc tagging:
> % python3 -c "import foo"
> % python3 -O -c "import foo"
> % python3 -OO -c "import foo"
> % ls foo/__pycache__/
> __init__.cpython-36.opt-1.pyc	__init__.cpython-36.pyc
> __init__.cpython-36.opt-2.pyc
> I also wonder how this would interact with pytest's um, 'hijacking' of
> the assert statement.

Pytest compiles from source, and uses its own cache tags which look like 


More information about the Python-ideas mailing list