Page 174 -
P. 174
python toolbox
Your Python Toolbox
You’ve got Chapter 4 under your The strip() method removes
belt and you’ve added some key
CHAPTER 4 unwanted whitespace from strings. CHAPTER 4
Python techiques to your toolbox.
The file argument to the print()
BIF controls where data is sent/saved.
The finally suite is always executed
no matter what exceptions occur within a
try/except statement.
An exception object is passed into the
Python Lingo an identifier using the as keyword.
except suite and can be assigned to
• “Immutable types” - data types The str() BIF can be used to access
in Python that, once assigned
the stringed representation of any data
a value, cannot have that value
object that supports the conversion.
changed. The locals() BIF returns a collection
of variables within the current scope.
• “Pickling” - the process of The in operator tests for membership.
saving a data object to persistence
storage. The “+” operator concatenates two
• “Unpickling” - the process of two numbers together when used with
strings when used with strings but adds
restoring a saved data object
numbers.
from persistence storage.
The with statement automatically
arranges to close all opened files, even
when exceptions occur. The with
statement uses the as keyword, too.
sys.stdout is what Python calls
“standard output” and is available from
the standard library’s sys module.
The standard library’s pickle module
lets you easily and efficiently save and
restore Python data objects to disk.
The pickle.dump() function saves
data to disk.
The pickle.load() function
restores data from disk.
138 Chapter 4