16.6: Chapter 7
( \newcommand{\kernel}{\mathrm{null}\,}\)
7.1 Module basics
1.
b. The code has ten
def
statements, each of which defines a function.
2.
a. Many modules consist of function definitions only. The functions are intended to be called in other modules.
3.
c.
area.cube(5)
is 6 * 5**2
, which is 6 * 25
, which is 150
.
4.
a. When importing a module from another file, the suffix .py is removed.
5.
b. The circle function must be called using the area variable that refers to the module.
6.
b. Adding the function to the area module makes the function easier to reuse in other programs.
7.2 Importing names
1.
a. This statement imports the sqrt function defined in the math module.
2.
b. A variable is defined for each function imported: sin, cos, and tan.
3.
b. A name error occurs when a variable name is not found, which might be due to a missing import statement.
4.
b. This statement replaces the previously imported circle function, even though
circle()
and area.circle()
have different parameters.
5.
a. Assigning the area variable replaces the previously imported module with the integer
51
. The variable can no longer be used to call functions in the area module.
6.
b. The most recent definition of
hello()
requires one argument, so this line would result in an error.
7.3 Top-level code
1.
b. Whenever this module is imported, Python will output
"Defining sphere function"
as a side effect.
2.
a. Even though nothing is printed, calling the sphere function ten million times causes a delay in the main program.
3.
a. All code is at the top level, and no functions are defined. Importing this module would run all code as a side effect.
4.
c. The variable
__name__
is the module's name, unless the module was run as the main program. In that case, __name__
is "__main__"
.
5.
a. Since test.py was imported, the value of
__name__
is "test"
.
6.
c. This line is found in many Python modules to check if the module was run as the main program.
7.4 The help function
1.
b. The digits have 0 red and 0 green, so the color must be a shade of blue. Navy is a darker version of standard blue,
#0000FF
.
2.
c. FF in hexadecimal is 255, the maximum amount of red, green, or blue in a color.
3.
a. The red, green, and blue values for orange are higher than the red, green, and blue values for green.
4.
b. The hexadecimal for YELLOW is
#FFFF00
, meaning 255 red + 255 green + 0 blue.
5.
c. Both the
darken
function and the BLUE
variable must be accessed via the colors
variable.
6.
a. Function names that begin with an underscore are intended to be used only within the current module.
7.
b. Only docstrings are included in the documentation. All comments are ignored by the help function.
8.
a. The functions are sorted alphabetically by name to make looking up a function easier.
9.
a. Functions that begin with an underscore are intended to be used only within a module, not in other modules.
7.5 Finding modules
1.
a. This reference describes the standard library, including built-in functions, types, and modules.
2.
b. As explained in the documentation of the
calendar
module, MONDAY
is 0
and SUNDAY
is 6
.
3.
a. The
tkinter
module stands for Tk interface. Tk is a standard toolkit for creating windows, text boxes, buttons, and other graphical widgets.
4.
c.
MoviePy
is a module for video editing, and Pillow
is a module for image processing.
5.
a. According to the "Features" section of the documentation,
Arrow
's is a "fully-implemented, drop-in replacement for datetime
."
6.
a. This function converts a color code to a color name. Ex:
hex_to_name("#daa520")
returns "goldenrod"
.