This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< 10.3 Try/Except | Contents | 10.5 Debugging >
Type Checking¶
Python is both a strongly and dynamically typed programming language. This means that any variable can take on any data type at any time (this is dynamically typed), but once a variable is assigned with a type, it can not change in unexpected ways. For example, you can write x = 1
immediately followed by x = "s"
, because it is a dynamically typed language. But you can not run "3" + 5
, because it is a strongly typed language (the string “3” can not convert in runtime to integer). In statically typed programming languages, you must declare what kind of data type your variable is to execute before you use it, and the data type usage of your variable cannot change within the scope of a function.
In the case of Python, there is no way to ensure that the user of your function is inputting variables of the data type you expect. For example, the function my_adder in Chapter 3 is designed to add three numbers together. However, the user can input strings, lists, dictionaries, or functions, each of which will cause different levels of problems. You can have your function type check the input variables before continuing and force an error using the error function.
TRY IT! Modify my_adder to type check that the input variables are floats. If any of the input variables are not floats, the function should return an appropriate error to the user using the raise function. Try your function for erroneous input arguments to verify that they are checked.
def my_adder(a, b, c):
# type check
if isinstance(a, float) and isinstance(b, float) and isinstance(c, float):
pass
else:
raise(TypeError('Input arguments must be floats'))
out = a + b + c
return out
my_adder(1.0, 2.0, 3.0)
6.0
my_adder(1.0, 2.0, '3.0')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-14e4b71b8c1d> in <module>
----> 1 my_adder(1.0, 2.0, '3.0')
<ipython-input-4-6073968d5755> in my_adder(a, b, c)
4 pass
5 else:
----> 6 raise(TypeError('Input arguments must be floats'))
7
8 out = a + b + c
TypeError: Input arguments must be floats
my_adder(1, 2, 3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-fc54adcab3d7> in <module>
----> 1 my_adder(1, 2, 3)
<ipython-input-4-6073968d5755> in my_adder(a, b, c)
4 pass
5 else:
----> 6 raise(TypeError('Input arguments must be floats'))
7
8 out = a + b + c
TypeError: Input arguments must be floats
We notice that 1, 2, 3 are integers instead of floats, therefore, it raise the error, we need to change the function to make sure that any numbers will be added.
def my_adder(a, b, c):
# type check
if isinstance(a, (float, int, complex)) and isinstance(b, (float, int, complex)) and isinstance(c, (float, int, complex)):
pass
else:
raise(Exception('Input arguments must be numbers'))
out = a + b + c
return out
my_adder(1, 2, 3)
6
my_adder(1.0, 2, 3)
6.0
my_adder(1j, 2+2j, 3+2j)
(5+5j)