Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 115 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 115 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 115 -

Bild der Seite - 115 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
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
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python