Read Programming Python Online

Authors: Mark Lutz

Tags: #COMPUTERS / Programming Languages / Python

Programming Python (3 page)

BOOK: Programming Python
13.11Mb size Format: txt, pdf, ePub
ads
Using Book Examples

Because this book’s examples form much of its content, I want to say
a few words about them up front.

Where to Look for Examples and Updates

As before, examples, updates, corrections, and supplements for
this book will be maintained at the author’s website, which lives
officially at the following URL:

http://www.rmi.net/~lutz/about-pp4e.html

This page at my book support website will contain links to all
supplemental information related to this version of the book. Because I
don’t own that domain name, though, if that link ceases to be during
this book’s shelf life, try the following alternative site as a fallback
option:

http://learning-python.com/books/about-pp4e.html
(alternative location)

If neither of those links work, try a general web search (which,
of course, is what most readers will probably try first anyhow).

Wherever it may live, this website (as well as O’Reilly’s,
described in the next section) is where you can fetch the book
examples distribution package
—an archive file
containing all of the book’s examples, as well as some extras that are
mentioned but not listed in the book itself. To work along without
having to type the examples manually, download the package, unpack it,
and consult its
README.txt
file for
usage details. I’ll describe how example labels and system prompts in
this book imply file locations in the package when we use our first
script in the first chapter.

As for the first three editions, I will also be maintaining an
informal “blog” on this website that describes Python changes over time
and provides general book-related notes and updates that you should
consider a supplemental appendix to this text.

O’Reilly’s website for this book, described later in this Preface,
also has an errata report system, and you can report issues at either my
site or O’Reilly’s. I tend to keep my book websites more up to date, but
it’s not impossible that O’Reilly’s errata page may supersede mine for
this edition. In any event, you should consider the union of these two
lists to be the official word on book corrections and updates.

Example Portability

The examples in this book were all developed, tested, and run
under Windows 7, and Python 3.1. The book’s major examples were all
tested and ran successfully on the upcoming Python 3.2, too (its alpha 3
release), just before the book went to the printer, so most or all of
this book applies to Python 3.2 as well. In addition, the C code of
Chapter 20
and a handful of
parallel programming examples were run under Cygwin on Windows to
emulate a Unix environment.

Although Python and its libraries are generally platform neutral,
some of this book’s code may require minor changes to run on other
platforms, such as Mac OS X, Linux, and other Unix variants. The tkinter
GUI examples, as well as some systems programming scripts, may be
especially susceptible to platform differences. Some portability issues
are pointed out along the way, but others may not be explicitly
noted.

Since I had neither time nor budget to test on and accommodate all
possible machines that readers might use over the lifespan of this book,
updates for platform-specific behaviors will have to fall into the
suggested exercises category. If you find a platform dependency and wish
to submit a patch for it, though, please see the updates site listed
earlier; I’ll be happy to post any platform patches from readers
there.

Demo Launchers

The book examples package described earlier also includes portable
example demo launcher scripts named PyDemos and PyGadgets, which provide
a quick look at some of this book’s major GUI- and Web-based examples.
These scripts and their launchers, located at the top of the examples
tree, can be run to self-configure program and module search paths, and
so can generally be run immediately on compatible platforms, including
Windows. See the package’s README files as well as the overviews near
the end of Chapters
6
and
10
for
more on these scripts.

Code Reuse Policies

We now interrupt this Preface for a word from the legal
department. This book is here to help you get your job done. In general,
you may use the code in this book in your programs and documentation.
You do not need to contact us for permission unless you’re reproducing a
significant portion of the code. For example, writing a program that
uses several chunks of code from this book does not require permission.
Selling or distributing a CD-ROM of examples from O’Reilly books does
require permission. Answering a question by citing this book and quoting
example code does not require permission. Incorporating a significant
amount of example code from this book into your product’s documentation
does require permission.

We appreciate, but do not require, attribution. An attribution
usually includes the title, author, publisher, and ISBN. For example:

Programming Python
, Fourth Edition, by Mark Lutz
(O’Reilly). Copyright 2011 Mark Lutz, 978-0-596-15810-1.”

Contacting O’Reilly

I described my own examples and updates sites in the prior section.
In addition to that advice, you can also address comments and questions
about this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States and Canada)
707-827-7000 (international/local)
707-829-0104 (fax)

As mentioned, O’Reilly maintains a web page for this book, which
lists errata, examples, and any additional information. You can access
this page at:

http://oreilly.com/catalog/9780596158101

To comment or ask technical questions about this book, send email
to:

[email protected]

For more information about books, conferences, software, Resource
Centers, and the O’Reilly Network, see the O’Reilly website at:

http://www.oreilly.com
Conventions Used in This Book

The following font conventions are used in this book:

Italic

Used for file and directory names, to emphasize new terms when
first introduced, and for some comments within code sections

Constant width

Used for code listings and to designate modules, methods,
options, classes, functions, statements, programs, objects, and HTML
tags

Constant width
bold

Used in code sections to show user input

Constant width italic

Used to mark replaceables

Note

This icon designates a note related to the nearby text.

Warning

This icon designates a warning related to the nearby text.

Acknowledgments

I acknowledged numerous people in the preface of
Learning
Python
, Fourth Edition, less than a year ago; because that book
is a precursor to this one, and because the set is largely the same, I
won’t repeat the list in its entirety here. In short, though, I’m grateful
to:

  • O’Reilly, for promoting Python, and publishing “meaty” books in
    the Open Source domain

  • The Python community, which has occupied sizeable portions of my
    world
    since 1992

  • The thousands of students who attended the 250 Python classes
    I’ve taught
    since 1997

  • The hundreds of thousands who read the 12 editions of the three
    Python books I’ve written since 1995

  • Monty Python, Python’s namesake, for so many great bits to draw
    from (more in the next chapter)

Although writing is ultimately a solitary task, the ideas that
spring forth owe much to the input of many. I’m thankful for all the
feedback I’ve been fortunate to receive over the last 18 years, both from
classes and from readers. Students really are the best teachers of
teachers.

On the (overly) personal front, I’d like to thank my brothers and
sister for old days, as well as my children, Michael, Samantha, and
Roxanne, for bragging rights.

And I’m especially thankful for my wife, Vera, who somehow managed
to append very good things to this otherwise immutable object.

—Mark Lutz, July 2010

So, What’s Python?

As discussed, this book won’t devote much space to Python
fundamentals, and we’ll defer an abstract discussion of Python roles
until the Conclusion, after you’ve had a chance to see it in action
firsthand. If you are looking for a concise definition of this book’s
topic, though, try this:

Python is a general-purpose, open source computer programming
language. It is optimized for software quality, developer productivity,
program portability, and component integration. Python is used by at
least hundreds of thousands of developers around the world in areas such
as Internet scripting, systems programming, user interfaces, product
customization, numeric programming, and more. It is generally considered
to be among the top four or five most widely-used programming languages
in the world today.

As a popular language focused on shrinking development time,
Python is deployed in a wide variety of products and roles. Counted
among its current user base are Google, YouTube, Industrial Light &
Magic, ESRI, the BitTorrent file sharing system, NASA’s Jet Propulsion
Lab, the game Eve Online, and the National Weather Service. Python’s
application domains range from system administration, website
development, cell phone scripting, and education to hardware testing,
investment analysis, computer games, and spacecraft control.

Among other things, Python sports a remarkably simple, readable,
and maintainable syntax; integration with external components coded in
other languages; a multi-
paradigm
design, with OOP, functional, and modular structures; and a vast
collection of precoded interfaces and utilities. Its tool set makes it a
flexible and agile language, ideal for both quick tactical tasks as well
as longer-range strategic application development efforts. Although it
is a general-purpose language, Python is often called a
scripting language
because it makes it easy to
utilize and direct other software
components
.

Perhaps Python’s best asset, though, is simply that it makes
software development more rapid and enjoyable. There is a class of
people for whom programming is an end in itself. They enjoy the
challenge. They write software for the pure pleasure of doing so and
often view commercial or career reward as secondary consequence. This is
the class that largely invented the Internet, open source, and Python.
This is also the class that has historically been a primary audience for
this book. As they’ve often relayed, with a tool like Python,
programming can be just plain
fun
.

To truly understand how, read on; though something of a side
effect, much of this book serves as a demonstration of Python’s ideals
in action in real-world code. As we’ll see, especially when combined
with toolkits for GUIs, websites, systems programming, and so on, Python
serves as
enabling technology
.

Part I. The Beginning

This part of the book gets things started by taking us on a quick
tour that reviews Python fundamental prerequisites and introduces some of
the most common ways it is applied.

Chapter 1

This chapter kicks things off by using a simple
example—recording information about people—to briefly introduce some
of the major Python application domains we’ll be studying in this
book. We’ll migrate the same example through multiple steps. Along
the way, we’ll meet databases, GUIs, websites, and more. This is
something of a demo chapter, designed to pique your interest. We
won’t learn the full story here, but we’ll have a chance to see
Python in action before digging into the details. This chapter also
serves as a review of some core language ideas you should be
familiar with before starting this book, such as data representation
and object-oriented programming (OOP).

The point of this part of the book is not to give you an in-depth
look at Python, but just to let you sample its application and to provide
you with a quick look at some of Python’s broader goals and
purposes.

Chapter 1. A Sneak Preview
“Programming Python: The Short Story”

If you are like most people, when you pick up a book as large as
this one, you’d like to know a little about what you’re going to be
learning before you roll up your sleeves. That’s what this chapter is
for—it provides a demonstration of some of the kinds of things you can do
with Python, before getting into the details. You won’t learn the full
story here, and if you’re looking for complete explanations of the tools
and techniques applied in this chapter, you’ll have to read on to later
parts of the book. The point here is just to whet your appetite, review a
few Python basics, and preview some of the topics to come.

To do this, I’ll pick a fairly simple application task—constructing
a database of
records—
and migrate it
through multiple steps: interactive coding, command-line tools, console
interfaces, GUIs, and simple web-based interfaces. Along the way, we’ll
also peek at concepts such as data representation, object persistence, and
object
-
oriented
programming (OOP); explore some
alternatives that we’ll revisit later in the book; and review some core
Python ideas that you should be aware of before reading this book.
Ultimately, we’ll wind up with a database of Python class instances, which
can be browsed and changed from a variety of interfaces.

I’ll cover additional topics in this book, of course, but the
techniques you will see here are representative of some of the domains
we’ll explore later. And again, if you don’t completely understand the
programs in this chapter, don’t worry because you
shouldn’t—
not yet anyway. This is just a
Python demo. We’ll fill in the rest of the details soon enough. For now,
let’s start off with a bit of fun.

Note

Readers of the Fourth Edition of
Learning
Python
might recognize some aspects of the running
example used in this chapter—the characters here are similar in spirit
to those in the OOP tutorial chapter in that book, and the later
class-based examples here are essentially a variation on a theme.
Despite some redundancy, I’m revisiting the example here for three
reasons: it serves its purpose as a review of language fundamentals;
some readers of this book haven’t read
Learning
Python
; and the example receives expanded treatment here,
with the addition of GUI and Web interfaces. That is, this chapter picks
up where
Learning Python
left off, pushing this
core language example into the realm of realistic applications—which, in
a nutshell, reflects the purpose of this book.

BOOK: Programming Python
13.11Mb size Format: txt, pdf, ePub
ads

Other books

Naked Empire by Terry Goodkind
Beneath a Southern Sky by Deborah Raney
The Road Home by Rose Tremain
If You See Her by Shiloh Walker
Crossbones Yard by Kate Rhodes
Man Without a Heart by Anne Hampson
Gilt Trip by Laura Childs