Seite - 17 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 17 -
Text der Seite - 17 -
1.4 ImportingfromModulesandPackages 17
chosen item names, makes it even more obvious that “name conflicts” is an issue
that shouldbeunderstood.
Several other coding alternatives would have helped the situation here. For ex-
ample, insteadoffrom math import *,wecouldswitch thestar (*)witha listof
itemnames, i.e. asfrom math import cos for thepresentversion.As longaswe
stayawayfrom(byamistake) importingexpalso frommath,nonameconflictwill
occur and the program will run fine. Alternatively, we could simply have switched
the order of the import statements (can you explain13 why?), or, we could have
moved the import statement from math import * down, so that it comes after
thestatementx = exp([0, 1, 2])andbefore theliney = cos(0).Notethat, in
Python3, importstatementsontheformfrom module import *areonlyallowed
atmodule level, i.e.,whenplaced inside functions, theygiveanerrormessage.
Next,wewill address thesafer “standard”wayof importing.
1.4.2 ImportingforUsewithPrefix
A safer implementation of our program would use the “standard” method of
importing, which we saw a glimpse of in ball_angle_prefix.py above. With
this import technique, thecodewould read
import numpy
import math
x = numpy.exp([0, 1, 2]) # do all 3 calculations
print(x) # print all 3 results
y = math.cos(0)
print(y)
We note that the import statementsareon the form
import some_library # i.e., items will be used with prefix
and that itemnamesbelongingtosome_libraryareprefixed withsome_library
and a “dot”. This means that, e.g.,numpy.exp([0, 1, 2]) refers to the (unique)
exp function from the numpy library. When the import statements are on the
“standard” form, the prefix is required.Leaving it out gives an error message. This
versionof the programrunsfine,producingtheexpectedoutput.
With the prefixing method, the order of imports does not matter, as there is no
doubtwherethefunctions(oritems)comefrom.Atthesametime, though,it isclear
that prefixing does not make it any easier for a human to read the “math meaning”
out of the code. In mathematical writing, there would be no prefix, so a prefix will
justcomplicatethejobforahumaninterpreter,andmoresothemorecomprehensive
theexpressionsare.
13 By switching the order, Python would first read from math import * and would import
everything, including exp, from math. Then, it would read from numpy import exp, which
would cause Python to import thenumpy version ofexp, which effectively means that themath
version ofexp is “overwritten” by theonefromnumpy.Atany laterpoint in thecode then,Python
will associate the wordexpwith thenumpy function.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik