Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What should be paid attention to when using assertions in python

Shulou Source: shulou.com Published: 2022-05-31 10:47:23 09月13日 Update

Most people do not understand the knowledge points of this article "what to pay attention to when using assertions in python", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what to pay attention to when using assertions in python" 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 ():" call the something function to implement some logic based on the result "" ret = something () assert len (ret) = = 18, "the number of list elements is incorrect" # what can python do? Python is a programming language with many effective tools built in. Python is almost omnipotent, the language is easy to understand, easy to start, powerful, and has a wide range of applications in many fields, such as big data analysis, artificial intelligence, Web development and so on.

The above is about the content of this article on "what to pay attention to when using assertions in python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Tags: Content function article knowledge program article language verification wrong powerful effective popular popular omnipotent artificial artificial intelligence value element global function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology OPPO Reno Linux macOS