Home  >  Article  >  Backend Development  >  Introduction to the usage of python assert (with code)

Introduction to the usage of python assert (with code)

不言
不言forward
2019-04-01 10:38:2823047browse

This article brings you an introduction to the usage of python assert (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

python assert The sentence format and usage are very simple. Usually the program throws an exception after running. Use assert to terminate the execution directly at the code where the exception occurs. Instead of waiting for the program to complete execution, an exception is thrown.

The role of python assert

python assert If an exception occurs, the expression is false. It can be understood that an exception will be triggered when the expression return value is false.

The syntax format of the assert statement

assert expression [, arguments]
assert 表达式 [, 参数]

Additional note: assert can also be used for multiple expressions Formula: assert expression1, expression2.
Note: When the expression = false, the exception following it will be executed.

Let’s look at a few examples
1: Single expression:

a = 1assert a < 0,
 '出错了,a大于0 啊'
 print('这里不会输出')

Output:

Traceback (most recent call last):
  File "main.py", line 3, in     
  assert a < 0, '出错了,a大于0 啊'
  AssertionError: 出错了,a大于0 啊

2: Multiple expressions:

a = 1
b = -1
assert a > 0, b < 0
print('正常输出,表达式返回真了') # 输出:正常输出,表达式返回真了

3: Try to capture assert Exception:

import traceback

try:
    assert a < 0
except AssertionError as aeeor:  # 明确抛出此异常
    # 抛出 AssertionError 不含任何信息,所以无法通过 aeeor.__str__()获取异常描述
    print('AssertionError', aeeor, aeeor.__str__())

    # 通过 traceback 打印详细异常信息
    print('traceback 打印异常')
    traceback.print_exc()
except:  # 不会命中其他异常
    print('assert except')

try:
    raise AssertionError('测试 raise AssertionError')
except AssertionError as aeeor:
    print('raise AssertionError 异常', aeeor.__str__())

Output:

AssertionError
traceback 打印异常
Traceback (most recent call last):
  File "main.py", line 7, in 
    assert a < 0
AssertionError
raise AssertionError 异常 测试 raise AssertionError

4: Function call throws exception:

# 除法运算
def foo(value, divide):
    assert divide != 0
    return value / divide


print('4除以2 =', foo(4, 2))  # 执行成功
print('4除以0 =', foo(4, 0))  # 抛出异常

Output:

4除以2 = 2.0
Traceback (most recent call last):
  File "main.py", line 8, in 
    print('4除以0 =', foo(4, 0))  # 抛出异常
  File "main.py", line 3, in foo
    assert divide != 0
AssertionError

Through the above examples, I believe everyone has a deep understanding of the use of aseert

Summary: When the expression returns false. Throw an exception directly to terminate execution and continue execution.

【Related recommendations: python video tutorial

The above is the detailed content of Introduction to the usage of python assert (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete