I love assert

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


-----BEGIN PGP SIGNED MESSAGE-----
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.

- --
~Ethan~
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBAgAGBQJUZtIBAAoJENZ7D1rrH75NmJAP/jmW9GhlR2TeThlUoiIaslVU
qKgiUeNplab9rbbIfQigmZaSTh/n+CeSK9pCu5dIOz7m6oBGoqtWJGFIl+E7pAsz
+MWLCDCIzCi4AGslDZ3ZZSW+e1zG2EK2BgNtLuh3EV4fBJq9GSNDfzlsUbXO7qnQ
yO0h41QuFOMuPiXDGkZ979u5OOBoH1JRUjmXhfUOSua0YcJ1dyRxNJKyhsMyUYPn
relMcTyT04rXj/sl3LUFgaO898mV7ieKgB5tBybAg5EKpArzpnAeIgMjcTP8B6YH
f+2n0jyL56oCCA0CO4BWP31unD8v89hBBeVJo0weprS9owq8OAdfyjXdfYMM77sM
oeQjCM8rs7uej17l4joGfvkrAnsa1BQLyhWzk0aeH4xpfMkgLHVnbwRt6v2PvPWK
IxNraSQQm3UjL1o1trLVznxT2+TOnNUucAl0XSLWSnIo9H1NJmYBZdHpmXvfo7R5
qIo1MyrQYLful+iaNI1I+5A3KLrKOduHm6Uqf4mDKXX6hMMbOdT1lHHpuD/fJYWW
jNqOiMpZl3NtxuEJjdLc5mvXvkoTZ9Z188WfFvFNnsL27aGxFfuulAWKc6EpbWgW
+8VoXVVhgs1mcB4UHM5dFrRSG7CgZi+btfHEGympWv74zun9FHHu2hNg+XPDibtS
D5P2/nCwQYFuCgapy+Ko
=QH9H
-----END PGP SIGNATURE-----



More information about the Python-list mailing list