1

I have been reading a bit about the assert statements in python and other languages. Particularly, I've read up on the assert in Python, Java and C. My understanding may not be a 100% correct, but the question that follows is about Python.

In the case of Python, why are assertions enabled by default? Why does python do this differently from other languages?** Is there a particular reason - compile time vs interpreted - for it?

  • In Java, assertions are disabled by default unless explicitly enabled.
  • In C and C++, programs using assertions from assert.h don't have them disabled by default but most Makefiles do define NDEBUG for releases/distributions.
    • CMake sets NDEBUG by default for dist
    • Many projects define their own assert Macro instead of using assert.h and change the default behavior.

I know my question is asking for a bit of historical context, but that is in fact the kind of answer that I am looking for.

Side note: I'm also new to python and where I work, we don't run any code in production with the optimization flags enabled. Is this a common practice when deploying python applications?

If you're interested, then below are the sources that I've read:

kapad
  • 611
  • 2
  • 11
  • 27

2 Answers2

2

My guess is that assertions are disabled in Java, C and C++ because of performance reasons. Asserts make the code slower and also might make some code optimizations impossible. Those languages have speed as a top priority, so it makes sense to disable them by default.

Python on the other hand is not concerned so much with speed. Have a look at the Python Zen:

Errors should never pass silently.
Unless explicitly silenced.

Assertions raise AssertionError, so better not pass them silently.

Chris
  • 2,461
  • 1
  • 23
  • 29
1

In the case of Python, why are assertions enabled by default? Why does python do this differently from other languages?** Is there a particular reason - compile time vs interpreted - for it?

Because generally speaking there's no reason not to. Python is not a fast language, and having code which looks like it should do things by default but does not is odd. Incidentally that is spreading to more language e.g. Rust has an assert! macro which always runs, and a separate debug_assert! which only runs in debug mode.

This property is also leveraged by the non-xunit testing packages like pytest which use and rewrite assertions in order to make them better (both more convenient and less constrainig) assert* methods.

Side note: I'm also new to python and where I work, we don't run any code in production with the optimization flags enabled. Is this a common practice when deploying python applications?

If you're asking whether running pyhton without -O is common, then yes. -O does relatively little that's useful: I don't think I've ever seen a codebase using __DEBUG__ and bypassing assert always feels icky. And -OO basically only does negative things.

The only cases I've seen which routinely use -O / -OO are:

  • Windows packaging where it does look semi-frequent
  • misguided attempts at obfuscation
Masklinn
  • 34,759
  • 3
  • 38
  • 57