Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the considerations for the use of assertions in python

Shulou Source: shulou.com Published: 2022-06-03 07:19:57 09月28日 Update

This article will explain in detail what matters needing attention in the use of assertions in python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. Assertions are mainly for debugging assistance, for program self-test, not for handling errors.

The program BUG still depends on try. Except will solve the problem.

2. Do not use assertions to validate user input, because when python runs from the command line, if you add the-O flag, the assertion will be disabled globally and all your validation will be lost.

Example

Def something (): "" this function does a lot of operations "" my_list = [] # declares an empty list # do somethingreturn my_list def func (): "" calls the something function Based on the results to achieve some logic "" ret = something () assert len (ret) = = 18, "the number of list elements is not correct" # finish some operations about "what are the precautions for the use of assertions in python" this article ends here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Articles items notes functions more programs validation no good practical elements global content commands instances quantity articles flags users knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn Linux Apple OPPO Reno