Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 17 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 17 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 17 -

Image of the Page - 17 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python