From the course: Secure Coding in Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Explicit assertions with Python

Explicit assertions with Python - Python Tutorial

From the course: Secure Coding in Python

Start my 1-month free trial

Explicit assertions with Python

- [Instructor] One of the many perks of using the Python language is what could be considered a built-in assertion engine. Assert is great for testing and debugging, but should never ever be used for high stakes production environments. Let's look at some code to see what this means. Here I am O2, O2, O2 begin in the file, simple_assertion.py. And straight off the bat I defined can_access as false. And then I do a try except where I assert in line four can_access. And since it's false, it's going to throw this assertion error, and in the except block in line six, we print "No access." Now, this seems like very predictable code, right? It's always false and it's always going to throw this "No access" exception block. Now, let's see what happens when I run this. So let me go over to my terminal. And here I am again at O2, 02, 02 begin. And let's do pipenv run python simple_assertion.py. And lo and behold, we have the "No…

Contents