Chapter 9 Flashcards
By default, command line arguments are stored…
The first element of this list is…
…in the list sys.argv, which is stored in the standard library sys module, with one string element for each argument.
…the name of the program executable
What is a usage message?
example
A usage message is implemented by the programmer so that when an error occurs relating to the number of arguments called, an example of the correct format to be used is printed.
if len(sys.argv[] != 3)
#print usage message here
sys.exit(1) #exit w/ error code 1
What is a script?
A script is a file of (Python) code that the programmer then passes to the interpreter as input.
What is a module?
A module is a file containing Python code that can be imported and used by different files/scripts (i.e.: import math).
The import function executes the module and thus defines its contents.
Explain what happens step by step when importing a module:
1). A check is performed to see if the module has already been imported. If it has, the already-imported module will be used.
2). If not already imported, a new module object is created and stored in sys.modules.
3).
What is true about the value of __name__ when using modules?
The __name__ variable of any imported module contains the name of the module.
The value of the __name__ variable of the executing script is always “__main__”
What is the purpose of the reload function and when might you use it?
The reload() function is used to refresh the contents of a module, and is best used in situations where information needs to be continuously updated, but it is not desirable to restart the entire program.
What is a package?
A package is a directory that, when imported, gives access to all modules located in the directory. This is favorable to importing each module one at a time.
Packages can contain subpackages, which themselves can contain modules.
How might you organize many different modules when dealing with a large project?
Sort them into packages that group related modules together.
Write a statement that imports a module “barn” from a subpackage “buildings”, which itself is contained in a package “ASCIIArt”.
from ASCIIArt.buildings import barn
This contains various utilities and tools for performing common program behaviors:
The Python standard library, which contains useful modules such as math and datetime.
What does file.read() do?
Returns the contents of a file as a string.
What does file.readlines() do?
Returns a list of strings, where the first element is the contents of the first line, the second element is the contents of the second line, and so on…
Both file.read() and file.readlines() can be given an optional argument that…
…specifies the number of bytes to be read
What does file.readline() do?
Returns the contents of a file one line at a time.