5.4 Making Our Own Module
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 understandable 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 As a simple test of the code in vertical_motion.py, we might compare
the output to hand calculations, as we did before. In Chap. 6, however, we will learn
how testing ought to be done via dedicated test functions. These test functions may
be run in different ways. One alternative, however, is to include an option within the
test block, which allows the user to run through the test functions whenever wanted,
but more about that later.
Placing Import Statements in Our Module
Note that if we have import statements in our module, it is possible to run into
trouble if we do not place them at the top of the file (which is according to 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 import of numpy is done).
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 if some_function is imported for use in another program.
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 understandable 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 As a simple test of the code in vertical_motion.py, we might compare
the output to hand calculations, as we did before. In Chap. 6, however, we will learn
how testing ought to be done via dedicated test functions. These test functions may
be run in different ways. One alternative, however, is to include an option within the
test block, which allows the user to run through the test functions whenever wanted,
but more about that later.
Placing Import Statements in Our Module
Note that if we have import statements in our module, it is possible to run into
trouble if we do not place them at the top of the file (which is according to 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 import of numpy is done).
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 if some_function is imported for use in another program.
