I love assert

Ethan Furman ethan at stoneleaf.us
Sat Nov 15 05:09:37 CET 2014

Hash: SHA1

On 11/14/2014 06:58 PM, Steven D'Aprano wrote:
> Ethan Furman wrote:
>> My point being:  a safety net that is so easily disabled does not count (IMHO) as a backup.
> Assertions are not a backup or a safety net. [...]

Would you be happier if I phrased that as: Defensive programming techniques that can be unknowingly disabled by the
end-user aren't very helpful?

>>> If your process is so poor that you release code without running it with asserts enabled, then assert will not
>>> save you from bugs.
>> This is one my very few complaints about Python:  running it normally is the same as running with DEBUG turned
>> on, so the unusual case is remembering to run with asserts turned /off/.
> That's curious. Your complaint is that asserts are too easy to disable, nevertheless it takes a deliberate and
> conscious act to disable them.

And more likely they were disabled because somebody was trying to get more performance from the app, not because that
someone thought, "I know, I'll turn asserts off and see how badly I can corrupt all my data."

> By default, Python runs assertions. And yet that is your complaint, so I suppose that you would rather asserts
> *didn't* run by default and you needed a deliberate act to run in debug mode.

That is correct.

> That doesn't seem consistent to me.

Because debug mode runs by default, many folks (myself included) simply run everything in debug mode.  I have never
even considered, until this very moment, to run my unittests with -O.  And yet the very purpose of -O is to
(hopefully) get more performance.  So you end up with folks using asserts inappropriately, yet their tests pass.  They
distribute their code.  Somebody has a slower machine, or gobs of data, so they decide to use -O.  Asserts are
disabled, something somewhere isn't checked, an error passes silently, the wrong thing happens... I'll you use your
imagination for what happens next.

Having debug mode be the default makes it too easy to screw up.

And yes, I am aware of at least one very large, complex, and modular system that uses Python, Javascript, CSS, HTML,
and some templating language I don't remember the name of.  It allows for third-party module integration.  It checks
third-party code with asserts.  It checks user-input with asserts.

I strongly suspect that if debug mode was not the default, this particular abuse wouldn't happen nearly as frequently
as it does.

- --
Version: GnuPG v1.4.11 (GNU/Linux)


More information about the Python-list mailing list