Programming Python with CDROM

Programming Python with CDROM




Programming Python is a classic O'Reilly Nutshell Handbook® describing the use of the Python programming/scripting language. Python is a popular scripting language freely available over the Net. Like Perl, Python is powerful, but easier to use than a traditional compiler language like C or C++. Although it is used mostly in UNIX environments (including Linux), it is available on Windows and Mac platforms as well. Unlike Perl, Python uses an object-oriented paradigm, making it a particularly useful scripting language for C++ programmers and the Windows/OLE and Mac environments. This book will serve the Python community as our Programming Perl book does for the Perl community.This book complements the online reference material provided with the Python releases. It is endorsed by the creator of Python, Guido van Rossum, who wrote the foreword. The CD-ROM included with the book contains Python 1.3 binaries for most popular UNIX platforms, as well as Linux, Windows, NT, and the Mac. This book is the most comprehensive Python user material available from any publisher. It contains a number of running examples, presented simply at first but becoming more complex as new issues appear. Examples describing Graphical User Interface (GUI) programming use the Tk language. (Tk is usually considered a part of the Tcl scripting language, but is in fact usable with other scripting languages like Perl and Python.)An appendix contains a separate short language tutorial.

Product Details

ISBN-13: 9781565921979
Publisher: O'Reilly Media, Incorporated
Publication date: 10/08/1996
Series: Nutshell Handbooks Series
Edition description: BK&CD-ROM
Pages: 902
Product dimensions: 7.05(w) x 9.39(h) x 1.78(d)

About the Author

Mark Lutz is a leading Python trainer, the author of Python’s earliest and best-selling texts, and a pioneering figure in the Python world.

Mark is the author of the three O’Reilly books: Learning Python, Programming Python, and Python Pocket Reference, all currently in fourth or fifth editions. He has been using and promoting Python since 1992, started writing Python books in 1995, and began teaching Python classes in 1997. As of Spring 2013, Mark has instructed 260 Python training sessions, taught roughly 4,000 students in live classes, and written Python books that have sold 400,000 units and been translated to at least a dozen languages.

Together, his two decades of Python efforts have helped to establish it as one of the most widely used programming languages in the world today. In addition, Mark has been in the software field for 30 years. He holds BS and MS degrees in computer science from the University of Wisconsin where he explored implementations of the Prolog language, and over his career has worked as a professional software developer on compilers, programming tools, scripting applications, and assorted client/server systems.

Mark maintains a training website ( and an additional book support site on the Web (

Read an Excerpt

Chapter 1: So What's Python?

"And Now for Something Completely Different.."

This book is about using Python, a public-domain programming language. In acronyms, Python is both a very-high-level language (VHLL), and an object-oriented dynamic language (OODL). As a preliminary definition, Python can be described as a new kind of language tool. For many users, its

  • Support for object-oriented development

  • Powerful programming constructs

  • Extendible and embeddable architecture

  • Remarkably clear syntax and coherent design

make it almost ideal as both a scripting interface for modern systems, and a stand-alone rapid- development language. For example, Python's object-oriented nature mixes well with frameworks written in C++. And as a standalone tool, Python is commonly used in domains such as system administration, graphical user interfaces, internet scripting, and database programming. We'll refine this description in a moment.

The Life of Python

Python was invented around 1990 by Guido van Rossum, at CWI in Amsterdam. Despite the reptiles, it's named after the BBC comedy series Monty Python's Flying Circus. Guido was also involved with the Amoeba distributed operating system and the ABC language. in fact, the original motivation for Python was to create an advanced scripting language for the Amoeba system.

But Python's design turned out to be general enough for a wide variety of domains. It's now used by a growing number of engineers around the world, in increasingly diverse roles. For instance, a number of companies use Python in commercial products, for tasks such as GUI development tools, WWW scripting, interactive television, rapid program development, on-site customization of C++ class libraries, and more.

Since it first appeared on the public, domain scene in 1991, Python has continued to attract a loyal following, and spawned a dedicated Internet newsgroup, comp.lang.python, in 1994. And as this book was being written, Python's homepage debuted on the WWW at

To help manage Python's growth, an informal organization aimed at supporting Python developers has begun taking shape: the Python Software Activity, or PSA for short. Despite its public domain status, Python is a well-supported system, thanks to the dedication of its inventor and the Python community. For example, the PSA facilitates Python workshops, and maintains a software and resources locator service.

"Buses considered harmful"

The PSA was originally formed in response to a thread on the Python newsgroup, which posed the semiserious question: "What would happen if Guido was hit by a bus?" Guido van Rossum still manages most new developments in Python, but the PSA and Python's user-base help support the language, work on extensions, etc. Given Python's popularity, and the PSA infrastructure, bus attacks seem less threatening now; of course I can't speak for Guido ...

Finally, Python is true freeware: there are no restrictions on copying it, or distributing it with your products. it comes with complete source code, a debugger and profiler, built-in interfaces to common external services, plus tools for adding other interfaces. System functions, GUls, and databases are supported "out of the box." Python programs run on most platforms, including nearly all flavors of UNIX, PCs (DOS, Windows, OS/2), the Macintosh, and others. And by the time you read this, Python should be part of most Linux distributions.

What's All the Excitement About?

Back to our description. Python has been called a "next-generation scripting language." This definition probably summarizes the language's distinctions, and the theme of this book, better than most. Although different people like different things about Python, there are some common reasons underlying its popularity. Some of the central points in the "Python philosophy" are:


    A scripting language doesn't have to be hard to read, write, and maintain. Issues of aesthetics and readability need not be sacrificed in the interest of utility. With the right tool, there's no reason to abandon normal standards of quality, even for "quick and dirty" code.


    An extension language doesn't have to have limited functionality. The design goals of embeddability and semantic power aren't necessarily contradictory: an extension tool can also be a full-featured programming language


    A dynamic language can be used for more than trivial tasks. There's no reason that a language can't both provide rapid response during the development cycle and also have features that make it useful for building more advanced systems.


    Object-oriented programming can be a useful paradigm, given the right tool. When easy to apply, OOP can be a powerful tool for structuring and reusing code. An object- oriented language doesn't necessarily also have to be complex or difficult to use.


    No language is an island. By providing both a powerful dynamic language, and well- defined interfaces to other languages, Python fosters hybrid systems that simultaneously leverage the rapid turnaround of Python, and the efficiency of C.

Python's integration support is a crucial property: as we'll see, much of Python's power comes from its open design, and its interfaces to external services. In fact, some consider Python's library of existing interfaces to be among its greatest assets. As we'll see, embedding APIs in a high-level language like Python makes them easier to use. Moreover, Python's integration tools make it practical to embed Python in products, and to apply paradigms such as rapid prototyping and rapid development.

But compared to other public-domain scripting languages, the first two points here- coherence and semantic power-may be Python's biggest distinctions. Aesthetic issues such as readability and design coherence are always hard to define, but crucial in a programming tool. As one Python user put it,

"Python looks like it was designed, not accumulated."
Python's inventor has done a extraordinary job of balancing the goals of simplicity and utility. We'll see that Python's clear syntax and high-level tools encourage the creation of easy-to-read, reusable software.

We'll also find that Python programs tend to resemble traditional languages such as C and Pascal, rather than scripting languages like Perl or Tcl. In fact, Python is something of a scripting language in the guise of a traditional language. Another observer summarized this fusion of ideas well:

"Python bridges the gap between scripting languages and C."
By providing a full-featured programming language and supporting modern development paradigms, Python brings programming tools used for more substantial systems to the scripting world. For example, C is poor for fast prototyping, and awk is almost useless for designing large systems, but Python does both well. In short, Python is a simple but powerful language, suitable both for "quick and dirty" scripts and medium-to-large-scale systems development.

Is it a "scripting language" or an "extension language"?

The terms "scripting language" and "extension language" are often used interchangeably to refer to an embedded, interpreted language component. Unfortunately, "scripting language" sometimes denotes system administration languages, used for writing shell tools. Since Python can be used in both roles, we'll use both terms too. But the term "scripting" isn't meant to imply that Python is just a shell tools language. For instance, we'll see that embedded Python code can take many forms: character strings, objects in module files, executable script files, and more.


Table of Contents

Part 1: Introducing Python

Chapter 1. So What's Python?
"And Now for Something Completely Different"
The Life of Python
What's All the Excitement About?
The Compulsory Features List
What's Python Good For?
What's Python Not Good For?

Chapter 2. A Sneak Preview
"Put Your Code Where Your Mouth Is"
Shell Tools Programming
Graphical User Interfaces
Data Structure Libraries
Persistent Information
Processing Text-Based Information
Extension Language Programming
And Whatever Else You Want to Use It For

Chapter 3. Getting Started
"Scenes from Life in the Real World"
Configuring Your Environment
Installing Python
And If All Else Fails
Using the Command-Line Interpreter
Using the Python Command Line
Platforms Supported Today
What's Next?

Part 2: Language Fundamentals

Chapter 4. Writing Basic Shell Tools
It Was a Dark and Stormy Night...
"Quick and Dirty" File Packing
Dissecting the Code
New Language Concepts
"Telling the Monkeys What to Do"

Chapter 5. Variations on a Theme
"How Shall I Code Thee? Let Me Count the Ways"
Packing With File Methods
Packing with Explicit Files
Packing with Counter Loops
Unpacking with Explicit Files
Unpacking with Line-by-Line Input
Unpacking Without File Methods
Crunching the Code (to Death)

Chapter 6. Adding a Functional Interface
"The Packing Scripts Go Public"
Part 1: "The Middleman"
Part 2: "The Unpacker on Steroids"
Part 3: "The Packer Hits the Big Time"
Building Systems with Functions

Chapter 7. Adding a Simple User Interface
"Go Ahead--Reuse My Software"
Running Scripts Versus Calling Functions
Running the Interface in batch Mode
Building Systems with Modules

Chapter 8. Adding Text-Based Menus
"On Today's Menu: Packing, Unpacking, and Fresh Spam"
Making Menus with Dictionaries
Making Menus with Lists
Built-In Types: Operators, Methods, and Modules
The Zen of Python: Namespaces are Dictionaries
Dictionaries and Keyword Arguments
Design Concepts: Do Modules Support OOP?

Chapter 9. Moving Menus to Classes
"Here's Your Script. Here's Your Script on OOP. Any Questions?"
A First Attempt: Generalized Menu Functions
So Who Needs Classes?
OOP in Action: Menus as Classes
Exploring Python Classes
Configuring Menu Data
Namespaces, Part 3: Classes and Instances
More on Zen: Class and Instance Namespace Dictionaries
Design Concepts: Implementing Generic Functions

Chapter 10. More Class Magic
More Bells, Whistles, and Little Blinking Lights
Deriving Menus from Simpler User Interaction
A Simple User-Interface Subclass: Back to Where We Started?
A Menu-Interface Subclass: Registering Methods
Inheriting from More than One Class
More on Exception Handling: Exception Lists
Making Menus Expandable: Overloading Operators
Summary: Python and the OOP Trinity
Design Exercise: Adding Logging and Security Extensions
Where's the Beef?
Welcome to the Middle of the Book!
A Quick Summary of Topics We've Covered So Far
Plus a Few Shell Tool Tricks
Python in a Nutshell
What's Next?
Brewing Python tee

Part 3: Tools and Applications

Chapter 11. Graphical User Interfaces
"Here's Looking at You, Kid"
Climbing the GUI Learning Curve
Automating GUI Construction
Case Study: "The Packer Goes GUI!"
Avoiding Namespace Clashes
Handling Program Errors
A Totally RAD Language
Other Tkinter Topics

Chapter 12. Persistent Information
"Give Me an Order of Persistence, But Hold the Pickles"
Case Study: a Table Browser GUI
Other Persist

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews