16
1 The First Few Steps
call. This sounds good to us, so based on what we were told, we start writing our
program as
from numpy import exp
x = exp([0, 1, 2])
# do all 3 calculations
print(x)
# print all 3 results
The script runs without any problems and the printed numbers seem fine,
[ 1.
2.71828183 7.3890561 ]
Moving on, we want to test a number of functions from the math library (cos, sin,
tan, etc.). Since we foresee testing quite many functions, we choose the “lazy”
import technique and plan to extend the code with one function at a time.
Extending the program with a simple usage of the cos function, it reads
from numpy import exp
from math import *
x = exp([0, 1, 2])
# do all 3 calculations
print(x)
# print all 3 results
y = cos(0)
print(y)
Running this version of the script, however, we get a longer printout (most of which
is irrelevant at this point) ending with
TypeError: a float is required
Clearly, something has gone wrong! But why?
The explanation is the following. With the second import statement, i.e.,
from math import *
all items from math were imported, including a function from math that is also
named exp. That is, there are two functions in play here that both go by the name
exp! One exp function is found in the numpy library, while the other exp function is
found in the math library, and the implementations of these two are different. This
now becomes a problem, since the last imported exp function silently “takes the
place” of the previous one, so that the name exp hereafter will be associated with
the exp function from math! Thus, when Python interprets x = exp([0, 1, 2]),
it tries to use exp from math for the calculations, but that version of exp can only
take a single number (real or integer) as input argument, not several (as exp from
numpy can). This mismatch then triggers the error message 12 and causes program
execution to stop before reaching y = cos(0).
Similar name conflicts may arise also with other functions than exp, since a lot
of items appear with identical names in different libraries (e.g., also cos, sin, tan,
and many more, exist with different implementations in both numpy and math).
The fact that programmers may create, and share, their own libraries containing self
12 It should be mentioned here, that error messages can not always be very accurate. With
some experience, however, you will find them very helpful at many occasions. More about error
messages later (Sect. 1.7).
1 The First Few Steps
call. This sounds good to us, so based on what we were told, we start writing our
program as
from numpy import exp
x = exp([0, 1, 2])
# do all 3 calculations
print(x)
# print all 3 results
The script runs without any problems and the printed numbers seem fine,
[ 1.
2.71828183 7.3890561 ]
Moving on, we want to test a number of functions from the math library (cos, sin,
tan, etc.). Since we foresee testing quite many functions, we choose the “lazy”
import technique and plan to extend the code with one function at a time.
Extending the program with a simple usage of the cos function, it reads
from numpy import exp
from math import *
x = exp([0, 1, 2])
# do all 3 calculations
print(x)
# print all 3 results
y = cos(0)
print(y)
Running this version of the script, however, we get a longer printout (most of which
is irrelevant at this point) ending with
TypeError: a float is required
Clearly, something has gone wrong! But why?
The explanation is the following. With the second import statement, i.e.,
from math import *
all items from math were imported, including a function from math that is also
named exp. That is, there are two functions in play here that both go by the name
exp! One exp function is found in the numpy library, while the other exp function is
found in the math library, and the implementations of these two are different. This
now becomes a problem, since the last imported exp function silently “takes the
place” of the previous one, so that the name exp hereafter will be associated with
the exp function from math! Thus, when Python interprets x = exp([0, 1, 2]),
it tries to use exp from math for the calculations, but that version of exp can only
take a single number (real or integer) as input argument, not several (as exp from
numpy can). This mismatch then triggers the error message 12 and causes program
execution to stop before reaching y = cos(0).
Similar name conflicts may arise also with other functions than exp, since a lot
of items appear with identical names in different libraries (e.g., also cos, sin, tan,
and many more, exist with different implementations in both numpy and math).
The fact that programmers may create, and share, their own libraries containing self
12 It should be mentioned here, that error messages can not always be very accurate. With
some experience, however, you will find them very helpful at many occasions. More about error
messages later (Sect. 1.7).
