Getting to Know Assertions

00:00 Getting to Know Assertions in Python.

00:04 Python implements a feature called assertions that are pretty useful during the development of your applications and projects. You’ll find this feature in several other languages too, such as C and Java, and it comes in handy for documenting, debugging, and testing your code.

00:19 If you’re looking for a tool to strengthen your debugging and testing process, then assertions are for you. In this part of the course, you’ll learn the basics of assertions, including what they are, what they’re good for, and when you shouldn’t use them in your code.

00:34 In Python, assertions are statements that you can use to set sanity checks during the development process. Assertions allow you to test the correctness of your code by checking if some specific conditions remain true, which can come in handy while you are debugging code.

00:49 The assertion condition should always be true unless you have a bug in your program. If the condition turns out to be false, then the assertion raises an exception and terminates the execution of the program. With assertions, you can set checks to make sure that invariants in your code stay invariant.

01:08 By doing so, you can check assumptions such as preconditions and postconditions. For example, you can set test conditions along the lines of This argument is not None or This return value is a string.

01:20 These kind of checks can help you catch errors as soon as possible when you’re developing a program.

01:27 Assertions are mainly for debugging. They’ll help you ensure that you don’t introduce new bugs while adding features and fixing other bugs in your code. However, they can have other interesting use cases within your development process.

01:41 These use cases include documenting and testing your code.

01:47 The primary role of assertions is to trigger the alarms when a bug appears in a program. In this context, assertions mean Make sure that this condition remains true. Otherwise, throw an error. In practice, you can use assertions to check preconditions and postconditions in your programs at development time. For example, programmers often place assertions at the beginning of functions to check if the input is valid (preconditions), and also before return values to check if the output is valid (postconditions). Assertions make it clear that you want to check if a given condition is and remains true. In Python, they can also include an optional message to unambiguously describe the error or problem at hand. That’s why they’re also an efficient tool for documenting code.

02:35 In this context, their main advantage is their ability to take concrete action instead of being passive, as comments and docstrings are.

02:45 Finally, assertions are also ideal for writing test cases in your code. You can write concise and to-the-point test cases because assertions provide a quick way to check if a given condition is met or not, which defines whether the test passes or fails.

03:00 You’ll learn more about these common use cases of assertions later on in the course, but what about when you shouldn’t use assertions?

03:08 In general, you shouldn’t use assertions for data processing or data validation because you can disable assertions in production code, which ends up removing all of your assertion-based processing and validation.

03:20 Using assertions in this way is a common pitfall, and you’ll learn more about this later on in the course. Additionally, assertions aren’t an error-handling tool.

03:29 The ultimate purpose of assertions isn’t to handle errors in production but to notify you during development, so you can fix them. In this regard, you shouldn’t write code that catches assertion errors using a regular tryexcept statement. In the next section of the course, you’ll get started by using assert statements.

Become a Member to join the conversation.