Why is the statement not used primarily?

I found that a Python statement statement is a good way to catch situations that should never be . And it can be removed by Python optimization when this code is trusted to be correct.

This seems to be the perfect mechanism for running Python applications in debug mode. But, looking at a few Python projects like django, twisted, and zope, assert almost never used. So why is this happening?

Why are statement statements not often used in the Python community?

+47
python debugging assert
Feb 01 2018-12-12T00:
source share
4 answers

I assume that the main reason assert not used most often is that no one uses Python optimized mode .

Asserts is a great tool for detecting programming errors, to protect against unforeseen situations, but all this error checking is cost-related. In compiled languages ​​such as C / C ++, this is not a big deal, since statements are only included in debug builds and are completely removed from releases.

In Python, on the other hand, there is no strict difference between debug and release mode. The interpreter has an "optimization flag" ( -O ), but currently it does not actually optimize the byte code, but only removes the statements.

Therefore, most Python users simply ignore the -O flag and run their scripts in "normal mode", which is a kind of debugging mode, since the statements are enabled, and __debug__ is True , but it is considered "production" ready.

Maybe it would be wiser to switch logic, i.e. “optimize” by default and enable only permissions in explicit debugging mode (*), but I assume that this will confuse many users, and I doubt that we will ever see such a change.

((*) This, for example, is how the Java VM does this using the -ea (enable assertions) switch.)

+48
Feb 03 2018-12-12T00:
source share

Several reasons come to mind ...

This is not the main function.

Many programmers, even if they are not bogged down in substantiation, do not respect everything that is not a direct participant in the penultimate functionality program. The assert statement is intended for debugging and testing, and thus the luxury that they can afford.

Device testing

The statement claims to be the growth and growth of unit testing. Although the assert statement is still in use, unit testing is now widely used to build a hostile environment in which bash crap from a routine and its system. In these conditions, the allegations claim that they begin to feel like knives in a shootout.

Improving industry respect for testing

The statement best serves as the last line of defense. He rose to high and untouchable heights under the C language, when this language ruled the world as a great way to implement the new "defensive programming"; he also recognizes the traps of catastrophic disasters at the moment when they sway on the edge. This was before the value of testing became widely recognized and respected, and disasters became much more common.

This is unheard of today, as any serious commercial software should be released without any form of testing. Testing is taken seriously and turned into a mass field. There are testing specialists and quality assurance departments with large checklists and official signatures. Under these conditions, programmers, as a rule, do not bother with statements because they are sure that their code will undergo such tedious testing that the chances of crazy conditions on the verge of extinction are so far that they can be neglected. This does not mean that they are right, but if the blame for lazy programming can be transferred to the QA department, hell, why not?

+30
Oct 13 '12 at 11:07
source share

I am not the author of any of these projects, so this is just an assumption based on my own experience. Without a direct request from people in these projects, you will not receive a specific answer.

The statement is excellent when you try to debug, etc. in your own application. However, as indicated in the link above, using conditional code is better when the application can predict and recover. I did not use zope, but in both Twisted and Django, their applications can recover and continue from many errors in your code. In a sense, they have already “compiled” statements, since they really can handle them.

Another reason for this is because often applications using external libraries, such as those listed by you, may want to handle errors. If the library simply uses assertions, no matter what the error is, it raises an AssertionError . With the conditional, libraries can really throw useful errors that can be captured and processed by your application.

+11
Feb 01 '12 at 17:53
source share

In accordance with my experience, assert widely used at the stage of program development - for checking user input. assert not needed to catch programming errors. Python itself is very good at catching genuine programming errors such as ZeroDivisionError, TypeError or so.

0
Nov 04 '17 at 2:42 on
source share



All Articles