Highlights from this chapter include:
- Programs can be organized into multiple .py files (modules). The
import
keyword allows a program to use functions defined in another .py file. - The
from
keyword can be used to import specific functions from a module. However, programs should avoid importing (or defining) multiple functions with the same name. - Modules often include the line
if __name__ == "__main__"
to prevent code from running as a side effect when the module is imported by other programs. - When working in a shell, the
help()
function can be used to look up the documentation for a module. The documentation is generated from the docstrings. - Python comes with over 200 built-in modules and hundreds of thousands of third-party modules. Programmers can search for modules on docs.python.org and pypi.org.
Statement | Description |
---|---|
|
Imports a module for use in another program. |
|
Imports a specific function from a module. |
|
A line of code found at the end of many modules. This statement indicates what code to run if the module is executed as a program (in other words, what code not to run if this module is imported by another program). |
|
Shows the documentation for the given module. The documentation includes the module's docstring, followed by a list of functions defined in the module, followed by a list of global variables assigned in the module, followed by the module's file name. |
|
Shows the docstring for the given function. |
|
Creates a |