Using the assert Keyword
In this lesson, you will learn about using the
assert keyword, which is a way in Python to quickly raise an exception if a condition isn’t true.
The way that it operates is that you want to assert that a certain condition is met—the one that follows the
assert keyword—and then if it is true, your code will continue. If it’s wrong, then an assertion exception is going to be raised.
Let’s look at it in some code. Like I mentioned, there’s an assertion exception,
AssertionError, that is built-in in Python as well. It’s one of those many built-in errors, so you could raise it just like you did before with your custom error.
In this check, you’re again looking whether the code is run on a Linux system, and if it isn’t, then you’re going to raise the
AssertionError with a message.
you will see that Python raised the
AssertionError and passed on the message. With the
assert keyword, you can create something that is very similar to this in just one line of code.
01:06 I’m going to show you how now, and in just a bit, I’m going to tell you why it’s just very similar and not exactly the same.
So, you could say
assert, using the keyword, then put in the check. So, this is the check that you want. You want to make sure
and then after a comma, you can say what’s the message that Python should pass to the
AssertionError if this condition is false. I’m going to say again
01:40 And if you compare these two, you can see that they’re quite similar, but this one is less code to write, and it has the same effect in the script that I’m running here currently.
So I’m going to say
python main.py, and you see, again, Python raises the
AssertionError, and it also has the message that you passed over here. Now, why is this different, a little bit, than the more explicit way that you wrote it before? And that’s about how
assert is implemented in Python.
02:11 You can think of it being implemented something like that. Let’s compare it to what you wrote below.
if not a: raise
b. This is what you have here.
if not sys.platform.startswith("linux"): raise
AssertionError with a message. So this is the same, right? But then you also have this
if __debug__. And this is not a problem if you run Python without any optimizations, but there’s ways to run your Python code with some optimizations enabled, which cuts out things like docstrings.
But it’s also just going to cut out any code that you write following an
assert keyword because this
__debug__ is not going to be around, so this whole assertion code is not going to execute.
So, this can be a problem. If you’re writing code that is meant to be optimized when it’s run, then you should definitely never use an
assert keyword to check for anything critical.
So you should only use
assert if it’s not critical for your program to run because you might want to eventually optimize the code when you’re running it in production, and then anything that uses the
assert keyword is just going to be completely skipped. So if it’s critical to the functioning of your program, then don’t use
assert but be more explicit and actually write out your exceptions.
Since in the code that you’re writing here, the check for which platform is it running on seems pretty critical for the code, I would advise against using the
assert keyword in here and just stick with being more explicit.
So, this is what the
assert keyword is for. As a debugging tool, you want to use it to throw in some quick checks for you as a developer to figure out whether some conditions are met, and stop execution if the conditions aren’t met. You don’t want to use it in production code, especially in code that you would want to optimize because if you run Python with optimizations, then your asserts that you write using the
assert keyword are just going to be skipped completely. So, that’s something that’s good to know.
And with that, we’re wrapping up the lesson on the
assert keyword, and in the next lesson, you’re going to start writing a
Become a Member to join the conversation.