Page - 119 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 119 -
Text of the Page - 119 -
5.4 MakingOurOwnModule 119
5.4.3 ModuleorProgram?
We know how a .py file can be executed as a program, and we have seen how
functions may be collected in a .py file, so that imports do not trigger any
undesirableprintouts.However,wehavealreadyrealizedthatPythondoesnot force
a.pyfile to be either a program, or a module. No, it can be both, and thanks to a
clever construction, Python allows a very flexible switch between the two ways of
usinga.pyfile.
Thiscleverconstructionisbasedonanif test,whichtestswhetherthefileshould
be run as a program, or act as a module only. This is doable by use of the variable
__name__, which (behindthescenes)Pythonsets toβ__main__βonly if thefile is
executed as a program(note the compulsory two underscores to each side ofname
andmainhere). We may put up a rather general form of the construction, that we
place in the.pyfile, as
< function definitions >
if __name__ == β__main__β: # note double underscores (and colon)
< statement 1 >
< statement 2 >
...
...
So, if the file is run as a program, Python immediately sets __name__ to
β__main__β. When reaching the if test, it will thus evaluate to True, which
in turn causes the corresponding (indented) statements, i.e., the statements of the
so-called test block, to be executed. To the contrary, if the file is used for imports
only, __name__ will not be set to β__main__β, the if test will consequently
evaluate toFalse, and thecorrespondingstatementsare notexecuted.
Often, thestatements in the test blockarebest placed inoneor several functions
(thendefinedabovetheif test, togetherwith theother functiondefinitions), so that
when the if test evaluates to True, one or more function calls will follow. This
is particularly important when different tasks are handled, so that each function
containsstatements that logicallybelong together.
As a simple illustration, when one function is natural (e.g., named
application), theconstructionmaybereformulatedas
< function definitions >
def application():
< statement 1 >
< statement 2 >
...
...
if __name__ == β__main__β:
application()
Our .py File as Both Module and Program We will now incorporate this
construction in vertical_motion.py. This allows us to use the functions from
vertical_motion.pyalso inaprogram(ourapplication) thatasks theuser for
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