7.6 Chapter summary
Highlights from this chapter include:
- Programs can be organized into multiple .py files (modules). The
importkeyword allows a program to use functions defined in another .py file. - The
fromkeyword 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
helpfunction 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 |
|---|---|
import module | Imports a module for use in another program. |
from module import function | Imports a specific function from a module. |
if __name__ == "__main__": | 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). |
help(module_name) | 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. |
help(function_name) | Shows the docstring for the given function. |
date(2023, 2, 14) | Creates a date object representing February 14, 2023.Requires: from datetime import date. |