April 2019
Intermediate to advanced
646 pages
16h 48m
English
Given the dynamic typing nature of Python, some developers use assertions at the top of their functions and methods to make sure the arguments have proper content, for example:
def divide(dividend, divisor):
assert isinstance(dividend, (int, float))
assert isinstance(divisor, (int, float))
return dividend / divisor
This is often done by developers who are used to static typing and feel that something is missing in Python.
This way of checking arguments is a part of the Design by Contract (DbC) programming style, where preconditions are checked before the code is actually run.
The two main problems in this approach are as follows: