Python small trick-assert assertion-exception handling (raise)

Python3 assert assertion


Python assert (assert) is used to judge an expression and trigger an exception when the expression condition is false.
Assertions can return an error directly when the conditions are not met when the program is running, instead of waiting for a crash after the program is running.

  • 1. For example:
assert express
  • 1. Equivalent to:
if not expression:
    raise AssertionError
  • 2. You can also bring parameters later:
assert expression [, arguments]
  • 2. Equivalent to:
if not expression:
    raise AssertionError(arguments)

Guess you like

Origin blog.csdn.net/fuhao7i/article/details/109568392