I love assert

Chris Angelico rosuav at gmail.com
Wed Nov 12 03:21:42 CET 2014

On Wed, Nov 12, 2014 at 7:03 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
> An ‘assert’ statement in the code will sometimes be active, and
> sometimes be a no-op, for *exactly* the same code under different
> circumstances. That's a trap for the reader, and I'd rather not set it.

This is no worse than other forms of preprocessor magic. I've seen
this kind of thing in plenty of C projects:

#define DEBUG print
#define DEBUG(...)

... further down ...
DEBUG("We're here: %d:%d", foo, bar);

If you're not in debug mode, this won't be evaluated. You can have
"active code" inside its args if you want to, but only if it's
exclusively part of the debugging output:

DEBUG("We've hit this line %d times", ++hitcount);

Again, not materially different from assert, and plenty of projects
have this. Maybe people just need to understand "assert == DEBUG" and
all's clear?


More information about the Python-list mailing list