Debugging Your Code With Assertions
00:00
Debugging Your Code With Assertions. At its core, the assert
statement is a debugging aid for testing conditions that should remain true during your code’s normal execution.
00:13
For assertions to work as a debugging tool, you should write them so that a failure indicates a bug in the code. In this section, you’ll learn how to use the assert
statement to assist you while debugging your code at development time.
00:27 You’ll typically use assertions to debug your code during development. The idea is to make sure that specific conditions are and remain true. If an asserted condition becomes false, then you immediately know you have a bug.
00:43
As an example, check out the Circle
class that’s seen on-screen.
01:05
The class initializer, .__init__()
, takes radius
as an argument and makes sure that the input value is a positive number. This check prevents circles with a negative radius.
01:19
The .area()
method computes the circle’s area. However, before doing that, the method uses an assert
statement to guarantee that .radius
remains a positive number. Why would you add this check? Well, let’s suppose you’re working on a team, and a coworker needs to add the code seen on-screen to Circle
.
01:43
This method takes a correction coefficient and applies it to the current value of .radius
. However, the method doesn’t validate the coefficient, introducing a subtle bug. Can you spot it?
01:58 Let’s say that the user provides a negative correction coefficient by accident.
02:13
The first call to .area()
works correctly because the initial radius is positive,
02:27
but the second call to .area()
breaks the code with an AssertionError
. Why? This happens because the call to .correct_radius()
turns the radius into a negative number, which uncovers a bug: the function doesn’t properly check for valid input.
02:45
In this example, the assert
statement works as a watchdog for situations in which the radius could take invalid values. The AssertionError
immediately points you to the specific problem: .radius
has unexpectedly changed to a negative number.
03:00
You have to figure out how this unexpected change happened and then fix your code before it goes into production. Developers often use assert
statements to check preconditions, just as you did in the previous example. Developers also use assertions to state postconditions.
03:17
For example, you can check if a function’s return value is valid right before returning the value to the caller. In general, the conditions that you check with an assert
statement should be true unless you or another developer in your team introduces a bug in the code.
03:33 In other words, these conditions should never be false. Their purpose is to quickly flag if someone introduces a bug. In this regard, assertions are early alerts in the code, which are meant to be useful during development.
03:48
To properly use assertions as a debugging tool, you shouldn’t use try
… except
blocks that catch and handle AssertionError
exceptions.
03:55 If an assertion fails, then your program should crash because a condition that was supposed to be true became false. You should not change this intended behavior by catching the exception.
04:08
A proper use of assertions is to inform developers about unrecoverable errors in a program. Assertions shouldn’t signal an expected error, such as a FileNotFoundError
, where a user can take a corrective action and try again.
04:21 The goal of an assertion should be to uncover programmers’ errors rather than users’ errors. Assertions are useful during the development process, not during production. By the time you release your code, it should mostly be free of bugs and shouldn’t require the assertions to work correctly. Finally, once your code is ready for production, you don’t have to explicitly remove assertions. You can just disable them, and that’s what you’ll be looking at in the next section of the course.
Become a Member to join the conversation.