I love assert

Ben Finney ben+python at benfinney.id.au
Wed Nov 12 03:35:53 CET 2014

Chris Angelico <rosuav at gmail.com> writes:

> 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.

That other languages do it doesn't argue in favour of it. It argues,
rather, that we should be glad not to have it in our Python code.

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

The more things people need to keep in mind when reading my code that
isn't stated explicitly in the code, the worse I consider the code to

Python is a small, clear, expressive language. It has the valuable
property that one does not need to keep in mind a lot of reminders of
unexpected behaviour; good Python code behaves as it explicitly says it
will, with very few implied surprises.

To preserve this valuable but fragile property, I recommend treating
‘assert’ as a clever but obfuscatory trick, opposed to that property of
Python. So an ‘assert’ statement should, IMO, not remain in the code
past a debugging session.

 \          “The entertainment industry calls DRM "security" software, |
  `\         because it makes them secure from their customers.” —Cory |
_o__)                                             Doctorow, 2014-02-05 |
Ben Finney

More information about the Python-list mailing list