Python asserts understanding and usage of assert

assert constitute assertion, mainly when we can write a new program, you can use it to help us lock bug range.

expression:

  assert expression 'window prompt information'

      The project parentheses are optional items, optional items will be thrown in the Boolean expression is False, it causes errors more clearly.

  E.g:

A =. 5 >>>
>>> = B. 6
>>> Assert A> B, 'A is not greater than B'
Traceback (MOST Recent Last Call):
  File "<pyshell # 26 is>", Line. 1, in <Module1>
    assert a> b, 'a no greater than b'
AssertionError with: b is not greater than A

Similarly, we can also use the function code that implements assert statement:

E.g:

  if not the expression:

    raise message

>>> a = 5
>>> b = 6
>>> if a < b:
 raise 'a不大于b'

Traceback (most recent call last):
  File "<pyshell#31>", line 2, in <module>
    raise 'a不大于b'
TypeError: exceptions must derive from BaseException
>>> if a > b :
 raise 'fadsfas'

>>>

for reference only!

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159836.htm
Recommended