Page - 115 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 115 -
Text of the Page - 115 -
5.4 MakingOurOwnModule 115
functionsdefinedin thatmoduleget readyforuse.Tosee that the functionynow is
readyforuse,we mayproceedour interactivesessionas:
In [2]: ball_function.y(v0=5, t=0.6) # remember to prefix y
Out[2]: 1.2342
Thus,apart fromtheundesirableprintouts, the import seems to work!
Torealizehowinappropriatethoseprintoutsare,wemightconsiderthefollowing
situation. A friend of yours wants to use your function y. You provide the file
ball_function.py, your friend importsball_function, and gets two numbers
printedonthescreen.Yourfrienddidnotaskforthosenumbers,andwouldprobably
endupreadingyourcode to seewhat theywereall about. It shouldnotbe like that.
Our main observation here, is that those undesirable printouts came from
statements placed outside of functions. Thus, the lesson learned, is seemingly that
whenpreparingmodulesforimport,thereshouldbenostatementsoutsidefunctions.
Or, could there be a way to treat such statements, so that undesirable printouts are
avoided?We will see.
These thoughtswillbe in thebackofourmindsaswenowproceedtodesign the
verticalmotionmodule.
Multiple Importsof the SameModule
Notethat,whenexecutingaprogram(orduringaninteractivesession),Python
does keep track of which modules that already have been imported. Thus, if
anotherimportis triedforacertainmodule,Pythonavoidsthetimeconsuming
and unnecessary task of executing the module file once again (all module
functionality is already in place, ready for use). You can check this out
if you like, by doing a second import ball_function. This time, there
are no printouts! Doing the import differently (i.e., with our example, as
from ball_function import y or from ball_function import *),
wouldnotmakeanydifference.
5.4.2 AModuleforVerticalMotion
One simple way to avoid undesirable printouts during import, is to let the module
file contain only function definitions. This is how we will arrange the first version
ofourverticalmotionmodule.
We proceed to make ourselves a preliminary version9 of our new module
file vertical_motion.py. In this file, we place three function definitions only
(which should suffice for our demonstration). One of these, is they function from
9 Note that only thefinal version, presented inSect.5.4.3, is found on the bookβs website.
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