Read it Now!
Reprint Licensing

Python Pocket Reference
Python Pocket Reference, Second Edition

By Mark Lutz

Cover | Table of Contents


Table of Contents

Chapter 1: Python Pocket Reference
Python is a general-purpose, object-oriented, and open source computer programming language. It is commonly used for both standalone programs and scripting applications in a wide variety of domains, by hundreds of thousands of developers.
Python is designed to optimize developer productivity, software quality, program portability, and component integration. Python programs run on most platforms in common use, including mainframes and supercomputers, Unix and Linux, Windows and Macintosh, Palm OS and WinCE, Java and .NET, and more.
This pocket reference summarizes Python statements and types, built-in functions, commonly used library modules, and other prominent Python tools. It is intended to serve as a concise reference tool for developers and is designed to be a companion to other books that provide tutorials, code examples, and other learning materials.
This second edition covers Python Release 2.2 and later. It has been thoroughly updated for recent language and library changes and expanded for new topics. Most of it applies to earlier releases as well, with the exception of recent language extensions.
[]
Something in brackets is usually optional.
*
Something followed by an asterisk can be repeated zero or more times.
a | b
Items separated by a bar are often alternatives.
italic
Used for filenames and URLs and to highlight new terms.
constant width
Used for code, commands, and command-line options, and to indicate the names of modules, functions, attributes, etc.
constant width italic
Used for replaceable parameter names in command syntax.
python [option*]
  [ scriptfilename | -c command | - ] [arg*]
-d
Turn on parser debugging output (for developers of the Python core).
-i
Enter interactive mode after executing a script or command, without reading the PYTHONSTARTUP file. Useful for postmortem debugging.
-O
Optimize generated byte-code (create and use .pyo byte-code files). Currently yields a minor performance improvement.
-OO
Like -O, but also removes docstrings from byte-code.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction
Python is a general-purpose, object-oriented, and open source computer programming language. It is commonly used for both standalone programs and scripting applications in a wide variety of domains, by hundreds of thousands of developers.
Python is designed to optimize developer productivity, software quality, program portability, and component integration. Python programs run on most platforms in common use, including mainframes and supercomputers, Unix and Linux, Windows and Macintosh, Palm OS and WinCE, Java and .NET, and more.
This pocket reference summarizes Python statements and types, built-in functions, commonly used library modules, and other prominent Python tools. It is intended to serve as a concise reference tool for developers and is designed to be a companion to other books that provide tutorials, code examples, and other learning materials.
This second edition covers Python Release 2.2 and later. It has been thoroughly updated for recent language and library changes and expanded for new topics. Most of it applies to earlier releases as well, with the exception of recent language extensions.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Conventions
[]
Something in brackets is usually optional.
*
Something followed by an asterisk can be repeated zero or more times.
a | b
Items separated by a bar are often alternatives.
italic
Used for filenames and URLs and to highlight new terms.
constant width
Used for code, commands, and command-line options, and to indicate the names of modules, functions, attributes, etc.
constant width italic
Used for replaceable parameter names in command syntax.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Command-Line Options
python [option*]
  [ scriptfilename | -c command | - ] [arg*]
-d
Turn on parser debugging output (for developers of the Python core).
-i
Enter interactive mode after executing a script or command, without reading the PYTHONSTARTUP file. Useful for postmortem debugging.
-O
Optimize generated byte-code (create and use .pyo byte-code files). Currently yields a minor performance improvement.
-OO
Like -O, but also removes docstrings from byte-code.
-S
Don't imply "import site" on initialization.
-t
Issue warnings about inconsistent tab usage (-tt issues error instead).
-u
Force stdout and stderr to be unbuffered and binary.
-U
Unicode literals: 'xxx' treated like u'xxx' (highly experimental in 2.1.1).
-v
Print a message each time a module is initialized, showing the place from which it is loaded; repeat this flag for more verbose output.
-x
Skip first line of source, allowing use of non-Unix forms of #!cmd.
-h
Print help message and exit.
-V
Print Python version number and exit.
-W arg
Warning control; arg is action:message:category:module:lineno. See warnings module documentation, new in 2.1, in the Python Library Reference (http://www.python.org/doc/).
scriptfilename
The name of a Python scriptfile to execute; the main, topmost file of a program, made available in sys.argv[0].
-c command
Specifies a Python command (as a string) to execute; sys.argv[0] is set to -c.
-
Read Python commands from stdin (the default); enter interactive mode if stdin is a tty.
arg*
Anything else on the command line is passed to the scriptfile or command (and appears in the built-in list of strings sys.argv[1:]).
If no scriptfilename or command is given, Python enters interactive mode, reading commands from stdin (and uses GNU readline, if installed, for input).
Besides traditional command lines, Python programs can also generally be started by clicking on their filenames in a file explorer GUI, by calling functions in the Python/C API, by using program launch menu options in IDEs such as IDLE and Komodo, and so on.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Environment Variables
PYTHONPATH
Augments the default search path for module files. The format is the same as the shell's PATH setting: directory pathnames separated by colons (semicolons on DOS). On module imports, Python searches for the corresponding file in each listed directory, from left to right. Merged into sys.path.
PYTHONSTARTUP
If set to the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode.
PYTHONHOME
If set, the value is used as an alternate prefix directory for library modules (or sys.prefix, sys.exec_prefix). The default module search path uses sys.prefix/lib.
PYTHONCASEOK
If set, ignore case in import statements (Windows, new in 2.1).
PYTHONDEBUG
If nonempty, same as -d option.
PYTHONINSPECT
If nonempty, same as -i option.
PYTHONOPTIMIZE
If nonempty, same as -O option.
PYTHONUNBUFFERED
If nonempty, same as -u option.
PYTHONVERBOSE
If nonempty, same as -v option.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Built-in Types and Operators
Table 1-1 lists Python's expression operators. Operators in the lower cells of this table have higher precedence (i.e., bind tighter) when used in mixed-operator expressions without parentheses.
Table 1-1: Expression operators and precedence
Operators
Description
lambda args: expr
Anonymous function maker
X or Y
Logical OR: Y is evaluated only if X is false
X and Y
Logical AND: Y is evaluated only if X is true
not X
Logical negation
X < Y, X <= Y, X > Y, 
X >= Y, X == Y, X <> Y, 
X != Y, X is Y, X is not Y, 
X in S, X not in S
Comparison operators, equality operators, inequality operators, object identity tests, sequence membership
X | Y
Bitwise OR
X ^ Y
Bitwise exclusive OR
X & Y
Bitwise AND
X << Y, X >> Y 
Shift X left, right by Y bits
X + Y, X—Y
Addition/concatenation, subtraction
X * Y, X % Y,
X / Y, X // Y
Multiply/repetition, remainder/format, division, floor division
-X, +X, ~X, X**Y
Unary negation, identity, bitwise complement, power
X[i], X[i:j], 
X.attr, X(...)
Indexing, slicing, attribute references, function calls
(...), [...], {...}, `...`
Tuple, list, dictionary, conversion to string
All built-in types support the comparisons and Boolean operations listed in Table 1-2.
Boolean true means any nonzero number or any nonempty collection object (list, dictionary, etc.). The special object None is false.
Comparisons return 1 or 0 and are applied recursively in compound objects as needed to determine a result.
Boolean and and or operators stop as soon as a result is known (short-circuit) and return one of the two operand objects (on left or right).
Table 1-2: Comparisons and Boolean operations
Operator
Description
X < Y
Strictly less than
X <= Y
Less than or equal
X > Y
Strictly greater than
X >= Y
Greater than or equal
X == Y
Equal (same value)
X != Y
Not equal (same as X< >Y)
X is Y
Same object
X is not Y
Negated object identity
X < Y < Z
Chained comparisons
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Specific Built-in Types
This section covers numbers, strings, lists, dictionaries, tuples, and files. Compound data types (e.g., lists, dictionaries, tuples) can nest inside each other arbitrarily.
This section covers basic number types (integers, floating-point), as well as more advanced types (complex, unlimited-precision long integers).

Section 1.6.1.1: Constants

Numbers are written in a variety of numeric constant forms:
1234, -24, 0
Normal integers (C longs, at least 32 bits)
99999999L, 42l
Long integers (unlimited size)
1.23, 3.14e-10, 4E210, 4.0e+210
Floating-point (C doubles)
0177, 0x9ff
Octal and hex integer constants
3+4j, 3.0+4.0j, 3J
Complex numbers

Section 1.6.1.2: Operations

Number types support all number operations (see Table 1-6). In mixed-type expressions, Python converts operands up to the type of the "highest" type, where integer is lower than long, which is lower than floating-point, which is lower than complex.
In 2.2, integer operations are automatically promoted to longs instead of overflowing, and there are two flavors of division (/ and //).
Strings are immutable (unchangeable) arrays of characters, accessed by offset.

Section 1.6.2.1: Constants

Strings are written as a series of characters in quotes:
"Python's", 'Python"s'
Double and single quotes work the same, and each can embed unescaped quotes of the other kind.
"""This is a
multiline block"""
Triple-quoted blocks collect lines into a single string, with end-of-line markers between the original lines.
'Python\'s\n'
Backslash escape code sequences (see Table 1-7) are replaced with the special-character byte values they represent (e.g., '\n' is a byte with binary value 012).
"This" "is" "concatenated"
Adjacent string constants are concatenated.
r'a raw\string', R'another\one'
Raw strings: backslashes are retained literally (handy for regular expressions and DOS directory paths; e.g., r'c:\dir1\file').
u"..."
Unicode string constants (see Section 1.6.2.4 later).
Table 1-7: String constant escape codes
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Statements and Syntax
This section describes the rules for syntax and variable names.
Here are the basic rules for writing Python programs:
Control flow
Statements execute one after another, unless control-flow statements are used (if, while, for, raise, calls, etc.).
Blocks
A block is delimited by indenting all of its statements the same amount, with spaces or tabs. A tab counts for enough spaces to move the column to a multiple of 8. Blocks can appear on the same line as a statement header if they are simple statements.
Statements
A statement ends at the end of a line, but may continue over multiple lines if a physical line ends with a \, an unclosed ( ), [], or {} pair, or an unclosed, triple-quoted string. Multiple simple statements can appear on a line if separated with a semicolon (;).
Comments
Comments start with a # (not in a string constant) and span to the end of the line.
Documentation strings
If a function, module file, or class begins with a string constant, it is stored in the object's _ _doc_ _ attribute. See the pydoc module and script in the Python Library Reference for automated extraction and display tools.
Whitespace
Generally significant only to the left of code, where indentation is used to group blocks. Blank lines and space are otherwise ignored except as token separators and within string constants.
This section contains the rules for user-defined names (i.e., variables) in programs.

Section 1.7.2.1: Name format

Structure
User-defined names start with a letter or underscore (_), followed by any number of letters, digits, or underscores.
Reserved words
User-defined names cannot be the same as any Python reserved word listed in Table 1-12.
Case sensitivity
User-defined names and reserved words are always case-sensitive: "SPAM", "spam", and "Spam" are all different names.
Unused tokens
Python does not use the characters @, $, or ? in its syntax, though they may appear in string constants and comments.
Creation
User-defined names are created by assignment but must exist when referenced. See Section 1.9 later in the book.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Specific Statements
The following sections describe all Python statements. Each section lists the statement's syntax formats, followed by usage details. For compound statements, each appearance of a suite in a statement format stands for one or more other statements, possibly indented as a block under a header line. A suite must be indented under a header if it contains another compound statement (if, while, etc.); otherwise, it can appear on the same line as the statement header. The following are both valid constructs:
if x < 42:
    print x
    while x: x = x -1

if x < 42: print x
Assignment
target = expression target1 = target2 = expression target1, target2 = expression1, expression2 target1, target2,... = same-length-sequence (target1, target2,...) = same-length-sequence [target1, target2,...] = same-length-sequence
Stores references to objects in targets. Expressions yield objects. Targets may be simple names (X), qualified attributes (X.attr), or indexes and slices (X[i], X[i:j]).
The second format assigns expression object to each target. The third format pairs targets with expressions, left to right. The last three formats assign components of any sequence to corresponding targets, from left to right. The sequence on the right must be the same length, but can be any type.
Augmented assignment
As of Python 2.0, a set of additional assignment statement formats, listed in Table 1-13, are available. Known as "augmented assignment," these formats imply a binary expression plus an assignment. For instance, the following two formats are roughly equivalent:
X = X + Y
X += Y
but the reference to target X needs to be evaluated only once, and in-place operations may be applied for mutables as an optimization (e.g., list1 += list2 automatically calls list1.extend(list2), instead of the slower concatenation operation implied by +). Classes may overload in-place assignments with method names that begin with an "i" (e.g., _ _iadd_ _ for +=, _ _add_ _ for +). The format X //= Y (floor division) is new in 2.2.
Table 1-13:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Namespace and Scope Rules
This section discusses rules for name binding and lookup. See also Section 1.7.2.1 and Section 1.7.2.2 earlier, in Section 1.7. In all cases, names are created when first assigned but must already exist when referenced. Qualified and unqualified names are resolved differently.
Qualified names (X, in object.X) are known as attributes and live in object namespaces. Assignments in some lexical scopes initialize object namespaces (modules, classes).
Assignment: object.X = value
Creates or alters the attribute name X in the namespace of the object being qualified.
Reference: object.X
Searches for the attribute name X in the object, then all accessible classes above it (for instances and classes). This is the definition of inheritance.
Unqualified names (X) involve lexical scope rules. Assignments bind such names to the local scope, unless they are declared global.
Assignment: X = value
Makes name X local: creates or changes name X in the current local scope by default. If X is declared global, creates or changes name X in the enclosing module's scope. Local variables are stored in the call stack for quick access.
Reference: X
Prior to Release 2.2, looks for name X in at most three scopes: the current local scope (function), then the current global scope (module), then the built-in scope (module _ _builtin_ _). Local and global scope contexts are defined in Table 1-16.
In Release 2.2 and later, looks for name X in the current local scope (function), then in the local scopes of all lexically enclosing functions (if any, from inner to outer), then in the current global scope (module), then in the built-in scope (module _ _builtin_ _). Global declarations make the search begin in the global scope instead.
Table 1-16: Unqualified name scopes
Code context
Global scope
Local scope
Module
Same as local
The module itself
Function, method
Enclosing module
Function call
Class
Enclosing module
class statement
Script, interactive mode
Same as local
module _ _main_ _
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Object-Oriented Programming
Classes are Python's main OOP tool. They support multiple instances, attribute inheritance, and operator overloading.

Section 1.10.1.1: Class objects provide default behavior

  • The class statement creates a class object and assigns it to a name.
  • Assignments inside class statements create class attributes, which export object state and behavior.
  • Class methods are nested defs, with special first arguments to receive the instance.

Section 1.10.1.2: Instance objects are generated from classes

  • Calling a class object like a function makes a new instance object.
  • Each instance object inherits class attributes and gets its own attribute namespace.
  • Assignments to attributes of the first argument (e.g, self.X = V) in methods create per-instance attributes.

Section 1.10.1.3: Inheritance rules

  • Inheritance happens at attribute qualification time: on object.attribute, if object is a class or instance.
  • Classes inherit attributes from all classes listed in their class statement header line (superclasses). Listing more than one means multiple inheritance.
  • Instances inherit attributes from the class from which they are generated, plus all that class's superclasses.
  • Inheritance searches the instance, then its class, then all accessible superclasses (depth-first, and left-to-right), and uses the first version of an attribute name found.
By default, all attribute names in modules and classes are visible everywhere. Special conventions allow some limited data hiding, but are mostly designed to prevent name collisions.See also Section 1.7.2.2 in Section 1.7.

Section 1.10.2.1: Module privates

Names in modules with a single underscore (e.g., _X), and those not listed on the module's _ _all_ _ list, are not copied over when a client uses from module import *. This is not strict privacy, though, as such names can still be accessed apart from the from..* statement.

Section 1.10.2.2: Class privates

In Python 1.5 and later, names anywhere within class statements with two leading underscores only (e.g., _ _X) are mangled at compile time to include the enclosing class name as a prefix (e.g.,
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Operator Overloading Methods
Classes intercept and implement built-in operations by providing specially named method functions, which all start and end with two underscores. These names are not reserved and may be inherited from superclasses as usual. At most one is located and called per operation.
Python automatically calls a class's overloading methods when instances appear in expressions and other contexts. For example, if a class defines a method named _ _getitem_ _, and X is an instance of this class, then the expression X[i] is equivalent to the method call X._ _getitem_ _(i).
Overloading method names are sometimes arbitrary: a class's _ _add_ _ method need not perform an addition (or concatenation). Moreover, classes generally may mix numeric and collection methods and mutable and nonmutable operations.
_ _init_ _(self [, arg]*)
On class(args...). Constructor: initializes the new instance, self.
_ _del_ _(self)
On instance garbage collection. Cleans up when instance is freed. Embedded objects are automatically freed when parent is (unless referenced from elsewhere).
_ _repr_ _(self)
On `self`, repr(self), print self (if no _ _str_ _). Returns string representation.
_ _str_ _(self)
On str(self), print self (or uses _ _repr_ _ if defined). Returns string representation.
_ _cmp_ _(self, other), _ _rcmp_ _
On self > x, x == self, cmp(self, x), etc. Called for all comparisons for which no more specific method (such as _ _lt_ _) is defined or inherited (see rich comparison methods below). Returns -1, 0, or 1 for self less than, equal to, or greater than other. If no rich comparison or _ _cmp_ _ methods are defined, class instances compare by their identity (address in memory). Note: _ _rcmp_ _ right-side method is no longer supported as of Release 2.1.
_ _hash_ _(self)
On dictionary[self], hash(self). Returns a unique and unchanging integer hash-key.
_ _call_ _(self [, arg]*)
On self(args...), when instance is called like a function.
_ _getattr_ _(self, name)
On self.name, when name is an undefined attribute access (not called if name exists in or is inherited by
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Built-in Functions
All built-in names (functions, exceptions, and so on) exist in the implied outer built-in scope, which corresponds to the _ _builtin_ _ module. Because this scope is always searched last on name lookups, these functions are always available in programs without imports. However, their names are not reserved words and may be hidden by assignments to the same name in global or local scopes.
abs(N)
Returns the absolute value of a number N.
apply(func, args [, keys])
Calls any callable object func (a function, method, class, etc.), passing the positional arguments in tuple args, and the keyword arguments in dictionary keys. Returns func call result.
buffer(object [, offset [, size]])
Returns a new buffer object for a conforming object (see the Python Library Reference).
callable(object)
Returns 1 if object is callable; else, returns 0.
chr(Int)
Returns a one-character string whose ASCII code is integer Int.
cmp(X, Y)
Returns a negative integer, zero, or a positive integer to designate (X < Y), (X == Y), or (X > Y), respectively.
coerce(X, Y)
Returns a tuple containing the two numeric arguments X and Y converted to a common type.
compile(string, filename, kind)
Compiles string into a code object. string is a Python string containing Python program code. filename is a string used in error messages (and is usually the name of the file from which the code was read, or <string> if typed interactively). kind can be exec if string contains statements; eval if string is an expression; or single, which prints the output of an expression statement that evaluates to something other than None. The resulting code object can be executed with exec statements or eval calls.
complex(real [, imag])
Builds a complex number object (can also be done using J or j suffix: real+imagJ). imag defaults to 0.
delattr(object, name)
Deletes the attribute named name (a string) from object. Similar to del obj.name, but name is a string, not a variable (e.g., delattr(a,'b') is like del a.b).
dir([object])
If no arguments, returns the list of names in the current local scope (namespace). With any object with attributes as an argument, returns the list of attribute names associated with that
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Built-in Exceptions
This section describes the exceptions that Python may raise during a program's execution. Beginning with Python 1.5, all built-in exceptions are class objects. Prior to 1.5, they were strings. Class exceptions are mostly indistinguishable from strings, unless they are concatenated. Built-in exceptions are defined in the module exceptions; this module never needs to be imported explicitly, because the exception names are provided in the built-in scope namespace. Most built-in exceptions have an associated extra data value with details.
Exception
Root superclass for all exceptions. User-defined exceptions may be derived from this class, but this is not currently enforced or required.
StandardError
Superclass for all other built-in exceptions except for SystemExit; subclass of the Exception root class.
ArithmeticError
Superclass for OverflowError, ZeroDivisionError, FloatingPointError; subclass of StandardError.
LookupError
Superclass for IndexError, KeyError; subclass of StandardError.
EnvironmentError
Superclass for exceptions that occur outside Python (IOError, OSError); subclass of StandardError. New in Release 1.5.2.
AssertionError
When an assert statement's test is false.
AttributeError
On attribute reference or assignment failure.
EOFError
When immediate end of file is hit by input( ) or raw_input( ).
FloatingPointError
On floating-point operation failure.
IOError
On I/O or file-related operation failure.
ImportError
On failure of import to find module or attribute.
IndexError
On out-of-range sequence offset (fetch or assign).
KeyError
On reference to nonexistent mapping key (fetch).
KeyboardInterrupt
On user entry of the interrupt key (often Ctrl-C).
MemoryError
On recoverable memory exhaustion.
NameError
On failure to find a local or global unqualified name.
NotImplementedError
On failure to define expected protocols. New in 1.5.2.
OSError
On os module error (its os.error exception). New in 1.5.2.
OverflowError
On excessively large arithmetic operation.
RuntimeError
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Built-in Attributes
Some objects export special attributes that are predefined by Python. The following is a partial list, because many types have unique attributes all their own; see the entries for specific types in the Python Library Reference.
X._ _dict_ _
Dictionary used to store object X's writable attributes.
I._ _methods_ _
List of instance object I's methods (name strings); available on many built-in types.
I._ _members_ _
List of instance object I's data attributes (name strings); available on many built-in types.
I._ _class_ _
Class object from which instance I was generated. In 2.2, this also applies to object types; most objects will have a _ _class_ _ attribute (e.g., []._ _class_ _ == list == type([])).
C._ _bases_ _
Tuple of class C's base classes, as listed in C's class statement header.
X._ _name_ _
Object X's name as a string; for classes, the name in the statement header; for modules, the name as used in imports, or "_ _main_ _" for the module at the top level of a program (e.g., the main file run to launch a program).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Built-in Modules
Built-in modules are always available but must be imported to be used in client modules. To access them, use one of these formats:
  • import module, and qualify module names (module.name)
  • from module import name, and use module names unqualified (name)
  • from module import *, and use module names unqualified (name)
For instance, to use name argv in the sys module, either use import sys and name sys.argv, or use from sys import argv and name argv.
There are hundreds of built-in modules; the next sections document the more commonly used ones. Listed export names followed by parentheses are functions that must be called; others are simple attributes (i.e., variable names in modules).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The sys Interpreter Module
The sys module contains interpreter-related exports. It also provides access to some environment components, such as the command line, standard streams, and so on.
argv
Command-line argument strings list: [command, arguments...]. Like C's argv array.
byteorder
Indicates the native byte-order (e.g., big for big-endian). New in 2.0.
builtin_module_names
Tuple of string names of C modules compiled into this Python interpreter.
copyright
String containing the Python interpreter copyright.
dllhandle
Python DLL integer handle; Windows only (see the Python Library Reference).
displayhook(func)
Called by Python to display results in interactive sessions; assign sys.displayhook to a one-argument function to customize output.
_ _displayhook_ _
Original value of displayhook (for restores).
excepthook(type, value, traceback)
Called by Python to display exception details to stderr; assign sys.excepthook to a three-argument function to customize exception displays.
_ _excepthook_ _
Original value of excepthook (for restores).
exc_info( )
Returns tuple of three values describing the exception currently being handled: (type, value, traceback). Specific to current thread. Subsumes exc_type, exc_value, and exc_traceback in Python 1.5 and later.
exc_type
Type of exception being handled (when an exception has been raised). Not thread-specific.
exc_value
Exception's parameter (second argument to raise). Not thread-specific.
exc_traceback
Exception's traceback object. Not thread-specific.
exec_prefix
Assigns a string giving the site-specific directory prefix where the platform-dependent Python files are installed; defaults to /usr/local or a build-time argument. Used to locate shared library modules (in <exec_prefix>/lib/python<version>/lib-dynload) and configuration files.
executable
String giving the file pathname of the Python interpreter program running the caller.
exit([N])
Exits from a Python process with status N (default 0) by raising SystemExit built-in exception (can be caught in a try statement and ignored). See also the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The string Module and Methods
The string module defines constants and variables for processing string objects, above and beyond string type operations. See also string built-in type operations and string-related built-in functions in Section 1.6, and Section 1.19.
As of Python 2.0, most functions in this module are also available as methods of string objects (see Section 1.6.2 in Section 1.6 for more details and a list of all available string methods). A string module function call such as func(str, arg) is generally now also accessible as str.func(arg). Where appropriate, both call formats are listed.
Note that as of Python 2.2, much of the string module is available as object methods, but not all; constants and some module functions are still available only in the string module. Function entries with just one listed call signature are not available as string methods. Similarly, some string methods are not available in this module (see the string type in Section 1.6).
digits
The string '0123456789'.
hexdigits
The string '0123456789abcdefABCDEF'.
letters
Concatenation of the strings lowercase and uppercase.
lowercase
Usually, the string 'abcdefghijklmnopqrstuvwxyz'.
octdigits
The string '01234567'.
printable
Combination of digits, letters, punctuation, and whitespace.
punctuation
String of characters that are considered punctuation characters.
uppercase
Usually, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
whitespace
String containing space, tab, linefeed, return, formfeed, and vertical tab.
In all of the following, the result is a new string (since strings are mutable, they are never modified in-place). Whitespace means spaces, tabs, and end-of-line characters (everything in string.whitespace).

Section 1.17.3.1: Conversions

atof(s)
Converts string s to floating-point. Like built-in float( ) function.
atoi(s [, base])
Converts string s to integer of given base (default is 10). Like built-in int( ) function.
atol(s [, base])
Converts string s to Python long integer (unlimited precision) of given base (default is 10). Like built-in
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The os System Module
The os module is the primary operating system (OS) services interface. It provides generic operating-system support and a standard, platform-independent OS interface. The os module includes tools for environments, processes, files, shell commands, and much more. It also includes a nested submodule, os.path, that provides a portable interface to directory processing tools.
Scripts that use os and os.path for systems programming are generally portable across most Python platforms. However, some os exports are not available on all platforms (e.g., fork is available on Unix but not Windows). Because the portability of such calls changes over time, consult the Python Library Reference for platform details.
Following are some miscellaneous module-related exports:
error
Known as both os.error and built-in OSError exception. Raised for os module-related errors. The accompanying value is a pair containing the numeric error code from errno and the corresponding string, as would be printed by the C function perror( ). See the module errno in the Python Library Reference for names of the error codes defined by the underlying OS.
When exceptions are classes, this exception carries two attributes: errno, the value of the C errno variable; and strerror, the corresponding error message from strerror( ). For exceptions that involve a file pathname (e.g., chdir( ), unlink( )), the exception instance also contains the attribute filename, the filename passed in.
name
Name of OS-specific modules whose names are copied to the top level of os (e.g., "posix", "nt", "dos", "mac", "os2", "ce", "java"). See also sys.platform in Section 1.16.
path
Nested module for portable pathname-based utilities. Example: os.path.split is a platform-independent directory name tool that internally uses an appropriate platform-specific call.
This section describes tools for parsing and building directory and search path strings portably. They are automatically set to the appropriate value for the platform on which a script is running. See also Section 1.18.8, later in the book, for additional portable filename-related tools.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The re Pattern-Matching Module
The re module is the standard regular expression-matching interface (new in 1.5). Regular expression (RE) patterns are specified as strings. This module must be imported.
compile(pattern [, flags])
Compile an RE pattern string into a regular expression object, for later matching. flags (combinable by bitwise | operator):
I or IGNORECASE or (?i)
Case-insensitive matching.
L or LOCALE or (?L)
Makes \w, \W, \b, \B, \s, \S, \d, and \D dependent on the current 8-bit locale (default is 7-bit U.S. ASCII).
M or MULTILINE or (?m)
Matches to each new line, not whole string.
S or DOTALL or (?s)
"." matches all characters, including newline.
U or UNICODE or (?u)
Makes \w, \W, \b, \B, \s, \S, \d, and \D dependent on Unicode character properties (new in 2.0).
X or VERBOSE or (?x)
Ignore whitespace in the pattern, outside character sets.
match(pattern, string [, flags])
If zero or more characters at start of string match the pattern string, returns a corresponding MatchObject instance, or None if no match. flags as in compile.
search(pattern, string [, flags])
Scans through string for a location matching pattern; returns a corresponding MatchObject instance, or None if no match. flags as in compile.
split(pattern, string [, maxsplit=0])
Splits string by occurrences of pattern. If capturing ( ) are used in pattern, occurrences of patterns or subpatterns are also returned.
sub(pattern, repl, string [, count=0])
Returns string obtained by replacing the (first count) leftmost nonoverlapping occurrences of pattern (a string or an RE object) in string by repl. repl can be a string or a function called with a single MatchObject argument, which must return the replacement string. repl may also include sequence escapes \1, \2, etc. to use substrings that matched groups, or \0 for all.
subn(pattern, repl, string [, count=0])
Same as sub, but returns a tuple: (new-string, number-of-subs-made).
findall(pattern, string)
Returns a list of strings giving all nonoverlapping matches of pattern in string. If one or more groups are present in the pattern, returns a list of groups. New in 1.5.2.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Object Persistence Modules
Three modules compose the object persistence interface:
anydbm
Key-based string-only storage files.
pickle (and cPickle)
Serializes an in-memory object to/from file streams.
shelve
Key-based persistent object stores: pickles objects to/from anydbm files.
The shelve module implements persistent object stores. shelve in turn uses the pickle module to convert (serialize) in-memory Python objects to byte-stream strings and the anydbm module to store serialized byte-stream strings in access-by-key files. See also Section 1.25 later in the book (not part of the standard library).
DBM is an access-by-key filesystem: strings are stored and fetched by their string keys. The anydbm module selects the keyed-access file implementation in your Python interpreter and presents a dictionary-like API for scripts. A persistent object shelve is used like a simple anydbm file, except that the anydbm module is replaced by shelve, and the stored value can be almost any kind of Python object (but keys are still strings).
import shelve
import anydbm
Gets dbm, gbmd, bsddb... whatever is installed.
file = shelve.open('filename')
file = anydbm.open('filename', 'c')
Creates a new or opens an existing dbm file.
file['key1'] = value
Store: creates or changes the entry for 'key1'.
value = file['key2']
Fetch: loads the value for the 'key2' entry.
count = len(file)
Size: returns the number of entries stored.
index = file.keys( )
Index: fetches the stored keys list (can use in a for).
found = file.has_key('key3')
Query: sees if there's an entry for 'key3'.
del file['key4']
Delete: removes the entry for 'key4'.
file.close( )
Manual close; required to flush updates to disk for some underlying DBM interfaces.

Section 1.20.1.1: Notes

  • dbm files and shelves work like dictionaries that must be opened before use; all mapping operations and some dictionary methods work.
  • For dbm files and shelves, can also pass mode ('r', 'w', etc.) and protection (access-mode) parameters to open if desired (some DBM flavors require extra arguments).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Tkinter GUI Module and Tools
Tkinter is a portable graphical user interface (GUI) construction library shipped with Python as a standard library module. Tkinter provides an object-based interface to the open source Tk library and implements native look and feel for Python-coded GUIs on Windows, X-Windows, and Mac OS. It is portable, simple to use, well-documented, widely used, mature, and well-supported.
In Tkinter scripts, widgets are customizable classes (e.g., Button, Frame), options are keyword arguments (e.g., text="press"), and composition is object embedding, not pathnames (e.g., Label(Top,...)).
from Tkinter import *               # widgets, constants

def msg(  ):                        # callback handler 
    print 'hello stdout...'

top = Frame(  )                     # make a container
top.pack(  )
Label(top,  text="Hello world").pack(side=TOP)
widget = Button(top, text="press", command=msg)
widget.pack(side=BOTTOM)
top.mainloop(  )
Table 1-20 lists the primary widget classes in the Tkinter module. These are true Python classes that may be subclassed and embedded in other objects. To cre