Chapter 17: Assertion & Design by Contract

Example 17.1, page no. 336

In [1]:
class Division():
    def assertcheck(self, a, b):
        assert(b!=0), "The value b cannot be zero"
        c = a/b
        print "The result is: ", c
div = Division()
div.assertcheck(5, 0)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-1-426e5021b6ba> in <module>()
      6         print "The result is: ", c
      7 div = Division()
----> 8 div.assertcheck(5, 0)

<ipython-input-1-426e5021b6ba> in assertcheck(self, a, b)
      2 class Division():
      3     def assertcheck(self, a, b):
----> 4         assert(b!=0), "The value b cannot be zero"
      5         c = a/b
      6         print "The result is: ", c

AssertionError: The value b cannot be zero