Why THIS is used in Python

preview_player
Показать описание
In this video, we will discuss the assert statements and how they can help in detect errors and bugs early in Python.

Python’s assert is mainly used for debugging by allowing us to write sanity tests in our code. These tests are performed to ensure that a particular condition is True or False. If the condition is False, an AssertionError is raised, indicating that the test condition failed.

The syntax of the `assert statement` is written in the following form:

`assert condition, error message`

`condition` – the condition or assumption to be tested

`message` – the message we want to display in the console when the condition is failed.

Chapters:
⏩ 0:00 Assert in Python
⏩ 0:29 Basic example
⏩ 0:52 Syntax
⏩ 1:07 Example
⏩ 1:28 Why do we assert statements?
⏩ 1:43 Example
⏩ 2:03 Outro

✨More on 2MinutesPy👇👇

@2MinutesPy

#debug #assertiveness #assert #debugging #debuggingtips #python
Рекомендации по теме
Комментарии
Автор

Nice, Short, crisp, complete topic, and great explanation with a great example 😃

hackytech
Автор

Maybe I missed it, but you should mention that assertions should only be used for debugging, never in production code. The python -O command removes assert, Python’s built-in __debug__ variable must be set to True when using assert statements. If it’s set to False, assert statements won’t execute during runtime. In production, you should use if conditions and raise exceptions instead.

dynamicgrad
Автор

Maybe not an exact 1:1, but this reminds me of setting types in typescript

CommDao
Автор

Very usefull. I used this in my python code, and there were no errors

prasal
Автор

So assert is a short form of: if not condition then raise specific error?

floriandymek
Автор

You've known the value of two minutes 😊

mohanasundaram
Автор

Hows this different from try and except or using logging func

seththunder
Автор

Should we really use it?? Btw, nice video

maxpythoneer
Автор

1:00 The conditoon is not expected to be true, but false! You fooled yourself earlier by choosing != as an example.

RagHelen