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 - 121 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 121 -

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

Text der Seite - 121 -

5.4 MakingOurOwnModule 121 position = y(v_initial, t) # compute all positions (over T) plt.plot(t, position, ’b--’) plt.xlabel(’Time (s)’) plt.ylabel(’Vertical position (m)’) plt.show() return if __name__ == ’__main__’: application() The code in application represents the main program and should be under- standable from what we have learned previously. Note that, like we have done here, it is usually a good idea to print some information about how the program works. Testing Asasimple testof thecodeinvertical_motion.py,wemightcompare theoutput tohandcalculations,aswedidbefore. InChap.6,however,wewill learn howtesting ought to be donevia dedicated test functions.These test functionsmay berunindifferentways.Onealternative,however, is to includeanoptionwithin the testblock,whichallowstheuser torunthroughthe test functionswheneverwanted, butmoreabout that later. PlacingImport Statements in Our Module Note that ifwehaveimportstatements inourmodule, it ispossible toruninto troubleifwedonotplace themat the topof thefile (whichisaccordingto the general recommendation). With the following sketchy example module, it will work fine to import some_function in another program and use it (since, when importing some_function, the importofnumpy isdone). import numpy as np def some_function(n): a = np.zeros(n) ... return r def application(): .... n = 10 r = some_function(n) ... return if __name__ == ’__main__’: application() One choice that would not work in the same way, however, would be to instead have the import statement import numpy as np after if __name__ == ’__main__’: . Then, this import statement would not be run ifsome_function is imported foruse inanotherprogram.
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