- Write Code!
- Check with
print
! - Contract with
assert
!
assert
's. And that's what this page is about.All hail
print
↠ assert
!An Inane Code Example
import random
x = random.randint(0, 9)
y = 2 * x
#print("x: %i, y: %i" % (x, y))
assert(y/2==x), "This is the definition."
assert(y%2==0), "y should be even."
assert(type(y)==int), "Very Bad News!"
- Number Doubling Code
- Visual check that all is well with
print
. - Make a lasting contract with
assert
. - Comment out
print
. - Move On!
It's not production grade. But then, I am not writing production grade code. I am writing scripts.
And that's about what I had to say. For years, I had been using
print
as my primary debugging tool. And now, I've added assert
as a built-in self-check, making it that much harder for me to break my own code.Also, I think it's more elegant.