Chapter 1. Python Pocket Reference
Introduction
Python is a general-purpose, multiparadigm, open source computer programming language, with support for object-oriented, functional, and procedural coding structures. It is commonly used both for standalone programs and for scripting applications in a wide variety of domains, and is generally considered to be one of the most widely used programming languages in the world.
Among Pythons features are an emphasis on code readability and library functionality, and a design that optimizes developer productivity, software quality, program portability, and component integration. Python programs run on most platforms in common use, including Unix and Linux, Windows and Macintosh, Java and .NET, Android and iOS, and more.
This pocket reference summarizes Python types and statements, special method names, built-in functions and exceptions, commonly used standard 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 fifth edition covers both Python 3.X and 2.X. It focuses primarily on 3.X, but also documents differences in 2.X along the way. Specifically, this edition has been updated to be current with Python versions 3.3 and 2.7 as well as prominent enhancements in the imminent 3.4 release, although most of its content also applies both to earlier and to later releases in the 3.X and 2.X lines.
This edition also applies to all major implementations of Pythonincluding CPython, PyPy, Jython, IronPython, and Stacklessand has been updated and expanded for recent changes in language, libraries, and practice. Its changes include new coverage of the MRO and super()
; formal algorithms of inheritance, imports, context managers, and block indentation; and commonly used library modules and tools, including json
, timeit
, random
, subprocess
, enum
, and the new Windows launcher.
Book Conventions
The following notational conventions are used in this book:
[]
In syntax formats, items in brackets are optional; brackets are also used literally in some parts of Pythons syntax as noted where applicable (e.g., lists).
*
In syntax formats, items followed by an asterisk can be repeated zero or more times; star is also used literally in some parts of Pythons syntax (e.g., multiplication).
a
|
b
In syntax formats, items separated by a bar are alternatives; bar is also used literally in some parts of Pythons syntax (e.g., union).
ItalicUsed for filenames and URLs, and to highlight new or important terms.
Constant width
Used for code, commands, and command-line options, and to indicate the names of modules, functions, attributes, variables, and methods.
Constant width italic
Used for replaceable parameter names in the syntax of command lines, expressions, functions, and methods.
Function()
Except where noted, callable functions and methods are denoted by trailing parentheses, to distinguish them from other types of attributes.
See Section Header Name
References to other sections in this book are given by section header text in double quotes.
Note
In this book, 3.X and 2.X mean that a topic applies to all commonly used releases in a Python line. More specific release numbers are used for topics of more limited scope (e.g., 2.7 means 2.7 only). Because future Python changes can invalidate applicability to future releases, also see Pythons Whats New documents, currently maintained at http://docs.python.org/3/whatsnew/index.html for Pythons released after this book.
Python Command-Line Usage
Command lines used to launch Python programs from a system shell have the following format:
python
[
option
*] [
scriptfile
| -c
command
| -m
module
| - ] [
arg
*]
In this format, python
denotes the Python interpreter executable with either a full directory path, or the word python
that is resolved by the system shell (e.g., via PATH
settings). Command-line options intended for Python itself appear before the specification of the program code to be run (option
). Arguments intended for the code to be run appear after the program specification (arg
).
Python Command Options
The option
items in ahead for 2.X differences):
-b
Issue warnings for calling str()
with a bytes
or bytearray
object and no encoding argument, and comparing a bytes
or bytearray
with a str
. Option -bb
issues errors instead.
-B
Do not write .pyc or .pyo byte-code files on imports.
-d
Turn on parser debugging output (for developers of the Python core).
-E
Ignore Python environment variables described ahead (such as PYTHONPATH
).
-h
Print help message and exit.
-i
Enter interactive mode after executing a script. Hint: useful for postmortem debugging; see also pdb.pm()
, described in Pythons library manuals.
-O
Optimize generated byte code (create and use .pyo byte-code files). Currently yields a minor performance improvement.
-OO
Operates like -O
, the previous option, but also removes docstrings from byte code.
-q
Do not print version and copyright message on interactive startup (as of Python 3.2).
-s
Do not add the user site directory to the sys.path
module search path.
-S
Do not imply import site on initialization.
-u
Force stdout and stderr to be unbuffered and binary.
-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.
-V
Print Python version number and exit (also available as --version
).
-W
arg
Warnings control: arg
takes the form action
:
message
:
category
:
module
:
lineno
. ).
-x
Skip first line of source, allowing use of non-Unix forms of #!
cmd
.
-X
option
Set implementation-specific option (as of Python 3.2); see implementation documentation for supported option
values.
Command-Line Program Specification
Code to be run and command-line arguments to send to it are specified in the following ways in Python command lines:
scriptfile
Denotes the name of a Python script file to run as the main, topmost file of a program (e.g., python
main.py
runs the code in main.py
). The scripts name may be an absolute or relative (to .) filename path, and is made available in sys.argv[0]
. On some platforms, command lines may also omit the