When do you use the ASSERT?

Asked 1 years ago, Updated 1 years ago, 128 views

Hello. I have a question, so I am posting it here.

I was reading a few Python codes and I saw something called an assert It's my first time seeing this keyword(?), so I'm looking for someone who can explain what it is.

Can you tell me what that is and when it is usually used?

python assert assertion

2022-09-22 10:18

1 Answers

The assert statement is a function not only in Python but also in other languages

Normal

assert condition

Write it like this and write it to the program as "If it doesn't fit this condition, give me an error!"

Exception handling considering python

if not condition:
    raise AssertionError()

You can think of it as the same as

assert works only in debugging mode and does nothing in the release It's similar to exception handling, but it's used in different situations.


2022-09-22 10:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.