116
5 Some More Python Essentials
ball_function.py, while the other two, time_of_flight and max_height,
compute the time of flight and maximum height attained, respectively (consult any
introductory book on mechanics regarding the implemented formulas).
In line with good programming practice, we also equip our module file with a doc
string on top. Generally, that doc string should give the purpose of the module and
explain how the module is used. More comprehensive doc strings are often required
for larger and more “complicated” modules (here, our doc string is very simple, but
professional programmers write their doc strings with great care 10 ). The module file
reads:
"""
Module for computing vertical motion
characteristics for a projectile.
"""
def y(v0, t):
"""
Compute vertical position at time t, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return v0*t - 0.5*g*t**2
def time_of_flight(v0):
"""
Compute time in the air, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return 2*v0/g
def max_height(v0):
"""
Compute maximum height reached, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return v0**2/(2*g)
# Other function definitions could be added here...
As with built-in modules, the built-in help function can be used to retrieve
documentation from user-defined modules:
In [1]: import vertical_motion
In [2]: help(vertical_motion)
Help on module vertical_motion:
NAME
vertical_motion
FILE
/.../.../.../vertical_motion.py
10 https://www.python.org/dev/peps/pep-0257/.
5 Some More Python Essentials
ball_function.py, while the other two, time_of_flight and max_height,
compute the time of flight and maximum height attained, respectively (consult any
introductory book on mechanics regarding the implemented formulas).
In line with good programming practice, we also equip our module file with a doc
string on top. Generally, that doc string should give the purpose of the module and
explain how the module is used. More comprehensive doc strings are often required
for larger and more “complicated” modules (here, our doc string is very simple, but
professional programmers write their doc strings with great care 10 ). The module file
reads:
"""
Module for computing vertical motion
characteristics for a projectile.
"""
def y(v0, t):
"""
Compute vertical position at time t, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return v0*t - 0.5*g*t**2
def time_of_flight(v0):
"""
Compute time in the air, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return 2*v0/g
def max_height(v0):
"""
Compute maximum height reached, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return v0**2/(2*g)
# Other function definitions could be added here...
As with built-in modules, the built-in help function can be used to retrieve
documentation from user-defined modules:
In [1]: import vertical_motion
In [2]: help(vertical_motion)
Help on module vertical_motion:
NAME
vertical_motion
FILE
/.../.../.../vertical_motion.py
10 https://www.python.org/dev/peps/pep-0257/.
