From the course: Secure Coding in Python

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Explicit assertions with Python

Explicit assertions with Python - Python Tutorial

From the course: Secure Coding in Python

Explicit assertions with Python

One of the features that Python includes out of the box is what essentially is a built-in assertion engine. Assert is great for testing and debugging, but it should really not be used for core business logic. Let me show you what I mean. So here I am in my Codespace and I'm going to navigate to 02_02_begin, simple assertion. And here you'll notice that I have can_access, which is false, and then I try to assert can_acces. And I have this assertion error, which is followed by a printing out of No access. So when this print runs, I should basically print No access. And let's go ahead and navigate to this. So cd 02_02_begin. Clear my terminal, and I'll run pipenv run python simple_assertion. py, and No access is printed. Now, this is what's going to happen most of the time, unless Python is run in a very specific way called optimized mode. Optimized mode removes assertions and some things that are related to debugging, in which case, this would not run the way we predict it to. Now…

Contents