python 3's adoption

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Jan 28 19:27:10 EST 2010


On Wed, 27 Jan 2010 23:50:55 -0800, Jonathan Gardner wrote:

> I agree on "assert". I don't like running a program in test mode and
> then running it in production mode with different code. I would rather
> test what I am going to actually run. "assert" should be a function, and
> support for removing assert statements should be eliminated.

But then it wouldn't be an assert, it would be a test, and tests are best 
written explicitly.

The whole point of assertions (outside of test code) is that they should 
ALWAYS pass. Since they should always pass, they're safe to optimize 
away, if the user explicitly runs Python with the "optimize" runtime 
switch. This is under the end-user's control, not the application writer. 
If you don't trust optimized code, don't use it.

Assertions should be treated as "this will never happen" code, and are 
only there to catch coding errors and logic mistakes. If you expect that 
a test could fail, it shouldn't be written as an assert but as an 
explicit test.

> I simply don't use assert statements at all.

Outside of explicit test code (e.g. unit tests or equivalent), asserts 
should be rare. If you never use them at all, it probably means you're 
either not programming defensively enough, or you're needlessly writing 
explicit tests to cover situations that can't happen. assert exists to 
cover the middle ground between those two extremes.


-- 
Steven



More information about the Python-list mailing list