DEV Community

Discussion on: 5 common beginner mistakes in Python

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

So Python has buit-in assertions, but if you want to actually use assertions, you have to use an external library instead?

Collapse
 
sanketsaurav profile image
Sanket Saurav

You can use assertions with the assert statement and that is the preferred way. The pattern here says you should not use assertions for some condition validation in your application logic — only use assertions in tests.

I hope that clarifies the point! :)

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

Somewhat, yes. However testing a condition and throwing an exception if it’s not met is exactly the kind of tedious task assertions were made to simplify, make easier to write and easier to read. So if you can’t use them in actual production code, it kinda defeats the purpose. Especially in a dynamically typed language where you have to type-check all your arguments.