What is AssertionError in Python

sape wrote:Why assert?

Hi sape!

`` assert '' reports my own personal mistakes that I make while programming.

For example, `` assert '' prevents me from passing the number 100 if I can only pass a number between 1 and 99.

Many of these tests are still very important when programming, but usually no longer make sense in the finished program. When programming, it is important to me whether the transferred parameter really has one or the other property. This way I can tell early if I pass something wrong.

`` assert '' has often drawn my attention to errors in the parameter transfer. That's why I often use it when the quality of the source code has to be very high and I want to notice errors in the source code fairly early on. Now you could name the typical duck typing argument, but I think you always have to weigh up and decide for yourself whether a more relaxed duck typing test or a strict isinstance is appropriate.

`` assert '' is not executed if Python is started with `` -O ''. However, one should also keep in mind that most Python programs are NOT run with `` -O ''.

If at some point you want to get rid of all the `` assert '' instructions, you can do it in a few minutes with an editor that can search.

`` assert '' has another advantage: This supports the code completion of WingIDE. If WingIDE does not know what type of data is available and therefore cannot offer code completion, then you can simply help with an `` assert (isinstance ()) ''. WingIDE can help you with programming.

Conclusion: `` assert '' helps me to avoid errors when programming. `` assert '' is not intended to catch user errors.


http://halvar.at | Small Bascom AVR course
Knowledge has a wonderful quality: it doubles when you share it.