Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Using the assert Keyword

00:00 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.

00:09 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.

00:24 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.

00:43 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.

00:51 If I run this,

00:55 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.

01:14 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 .platform.startswith("linux"),

01:27 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 "Wrong OS!".

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.

01:51 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.

02:20 if not a: raise AssertionError with 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.

02:47 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.

03:01 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.

03:11 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.

03:34 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.

03:49 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.

04:18 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 tryexcept block.

Become a Member to join the conversation.