Home Page Icon
Home Page
Table of Contents for
Learning Python
Close
Learning Python
by David Ascher, Mark Lutz
Learning Python
Learning Python
Preface
About This Book
This Book’s Scope
This Book’s Style
Part I
Part II
Part III
Prerequisites
Book Updates
Font Conventions
About the Programs in This Book
How to Contact Us
Acknowledgments
Mark Also Says:
David Also Says:
I. The Core Language
1. Getting Started
Why Python?
An Executive Summary
It’s object-oriented
It’s free
It’s portable
It’s powerful
It’s mixable
It’s easy to use
It’s easy to learn
Python on the Job
System utilities
GUIs
Component integration
Rapid prototyping
Internet scripting
Numeric programming
Database programming
And more: Image processing, AI, distributed objects, etc.
Python in Commercial Products
Python Versus Similar Tools
How to Run Python Programs
The Interactive Command Line
Running Module Files
Running Unix-Style Scripts
Embedded Code and Objects
Platform-Specific Startup Methods
What You Type and Where You Type It
A First Look at Module Files
A First Look at Namespace Inspection
Python Configuration Details
Environment Variables
An Example Startup Script
A GUI Test Session
Installation Overview
Summary
Exercises
2. Types and Operators
Python Program Structure
Why Use Built-in Types?
Numbers
Standard Numeric Types
Built-in Tools and Extensions
Python Expression Operators
Mixed operators: Operators bind tighter lower in the table
Parentheses group subexpressions
Mixed types: Converted up just as in C
Preview: operator overloading
Numbers in Action
Basic operations
B itwise operations
L ong integers
Complex numbers
Other numeric tools
Strings
Strings in Action
Basic operations
Indexing and slicing
C hanging and formatting
Common string tools
String constant variations
Generic Type Concepts
Types share operation sets by categories
Mutable types can be changed in place
Lists
Lists in Action
Basic operations
In dexing and slicing
Changing lists in place
Dictionaries
Dictionaries in Action
Basic operations
Changing dictionaries
A marginally more real example
Dictionary Usage Notes
Tuples
Why Lists and Tuples?
Files
Files in Action
Related Python Tools
General Object Properties
Type Categories Revisited
Generality
Shared References
Comparisons, Equality, and Truth
Python’s Type Hierarchies
Built-in Type Gotchas
Assignment Creates References, Not Copies
Solutions
Repetition Adds One-Level Deep
Solutions
Cyclic Data Structures Can’t Be Printed
Solutions
Immutable Types Can’t Be Changed in Place
Solutions
Summary
Exercises
3. Basic Statements
Assignment
Variable Name Rules
Expressions
Print
The Python “Hello World” Program
if Tests
General Format
Examples
Python Syntax Rules
B lock delimiters
S tatement delimiters
A few special cases
Truth Tests Revisited
while Loops
General Format
Examples
break, continue, pass, and the Loop else
General loop format
Examples
for Loops
General Format
Examples
range and Counter Loops
Examples
Common Coding Gotchas
Summary
Exercises
4. Functions
Why Use Functions?
Function Basics
General Form
Definitions and Calls
Example: Intersecting Sequences
Definition
Calls
Scope Rules in Functions
Name Resolution: The LGB Rule
Example
The global Statement
Argument Passing
More on return
Special Argument-Matching Modes
A first example
A second example: Arbitrary-argument set functions
The gritty details
Odds and Ends
lambda Expressions
The apply Built-in
The map Built-in
Python “Procedures”
Function Design Concepts
Functions Are Objects: Indirect Calls
Function Gotchas
Local Names Are Detected Statically
Solution
Nested Functions Aren’t Nested Scopes
Solution
Using Defaults to Save References
Defaults and Mutable Objects
Solution
Summary
Exercises
5. Modules
Why Use Modules?
Module Basics
Definition
Usage
Module Files Are Namespaces
Name Qualification
Import Model
Imports Happen Only Once
import and from Are Assignments
Reloading Modules
General Form
Example
Odds and Ends
Module Compilation Model
Data Hiding Is a Convention
Mixed Modes: __name__ and __main__
Changing the Module Search Path
Module Packages (New in 1.5)
Module Design Concepts
Modules Are Objects: Metaprograms
Module Gotchas
Importing Modules by Name String
Solution
from Copies Names but Doesn’t Link
Solution
Statement Order Matters in Top-Level Code
Solution
Recursive “from” Imports May Not Work
Solutions
reload May Not Impact from Imports
Solution
reload Isn’t Applied Transitively
Solution
Summary
Exercises
6. Classes
Why Use Classes?
Class Basics
Classes Generate Multiple Instance Objects
Class objects provide default behavior
Instance objects are generated from classes
An example
Classes Are Specialized by I nheritance
An example
Classes Can Intercept Python Operators
An example
Using the Class Statement
General Form
Example
Using Class Methods
Example
Inheritance Searches Namespace Trees
Attribute Tree Construction
Specializing Inherited Methods
Operator Overloading in Classes
Common Operator Overloading Methods
Examples
__getitem__ intercepts all index references
__getattr__ catches undefined attribute references
_ _repr__ returns a string representation
Namespace Rules: The Whole Story
Unqualified Names: Global Unless Assigned
Qualified Names: Object Namespaces
N amespace Dictionaries
Designing with Classes
Python and OOP
OOP and Inheritance: “is-a”
OOP and Composition: “has-a”
OOP and Delegation
Extending Built-in Object Types
Multiple Inheritance
Classes Are Objects: Generic Object Factories
Methods Are Objects: Bound or Unbound
Odds and Ends
Private Attributes (New in 1.5)
Documentation Strings
C lasses Versus Modules
Class Gotchas
Changing Class Attributes Can Have Side Effects
Solution
Multiple Inheritance: Order Matters
Solution
Class Function Attributes Are Special
Solution
Methods, Classes, and Nested Scopes
Solution
Summary
Exercises
7. Exceptions
Why Use Exceptions?
Exception Basics
try/except/else
try/finally
raise
First Examples
Default behavior: Error messages
Catching built-in exceptions
Raising and catching user-defined exceptions
Exception Idioms
Exceptions Aren’t Always a Bad Thing
Searches Sometimes Signal Success by raise
Outer try Statements Can Debug Code
Exception Catching Modes
try Statement Clauses
Catching 1-of-N Exceptions
Exceptions Nest at Runtime
finally Clauses Run “On the Way Out”
Odds and Ends
Passing Optional Data
The assert Statement
Class Exceptions
General raise forms
Example
Exception Gotchas
Exceptions Match by Identity, Not Equality
Catching Too Much?
Solution
Catching Too Little?
Solution
Summary
Exercises
II. The Outer Layers
8. Built-in Tools
Aside: The sys Module
Built-in Functions
Conversions, Numbers, and Comparisons
Attribute Manipulation
Executing Programs
Library Modules
Basic String Operations: The string Module
Advanced String Operations: The re Module
Regular expressions
A real regular expression problem
Generic Operating-System Interfaces: The os Module
The os and os.path modules
Copying Files and Directories: The shutil Module
Internet-Related Modules
The Common Gateway Interface: The cgi module
Manipulating URLs: the urllib and urlparse modules
Specific Internet protocols
Processing Internet data
Dealing with Binary Data: The struct Module
Debugging, Timing, Profiling
Exercises
9. Common Tasks in Python
Data Structure Manipulations
Making Copies Inline
Making Copies: The copy Module
Sorting and Randomizing
Randomizing: The random Module
Making New Data Structures
Making New Lists and Dictionaries: The UserList and UserDict Modules
Manipulating Files
Doing Something to Each Line in a File
Choosing chunk sizes
Doing Something to a Set of Files Specified on the Command Line
The f ileinput Module
Filenames and Directories
Matching Sets of Files: The glob Module
Using Temporary Files
More on Scanning Text Files
Manipulating Programs
Calling Other Programs
Internet-Related Activities
Downloading a Web Page Programmatically
Checking the Validity of Links and Mirroring Web Sites: webchecker.py and Friends
Checking Mail
Bigger Examples
Compounding Your Interest
An Automated Dial-Out Script
An Interactive Rolodex
Exercises
10. Frameworks and Applications
An Automated Complaint System
Interfacing with COM: Cheap Public Relations
A Tkinter-Based GUI Editor for Managing Form Data
Design Considerations
JPython: The Felicitous Union of Python and Java
JPython Gives Python Programmers Access to Java Libraries
JPython as a Java Scripting Language
A Real JPython/Swing Application: grapher.py
Other Frameworks and Applications
Python Imaging Library (PIL)
Numeric Python (NumPy)
SWIG
Python MegaWidgets (Pmw)
ILU and Fnorb
Exercises
III. Appendixes
A. Python Resources
The Python Language Web Site
Python Software
The Standard Python Distribution
Gifts from Guido
Offerings from Commercial Vendors
Scriptics Corporation’s Tcl/Tk toolkits
Digital Creation’s free releases
Pythonware
Other Modules and Packages
Emacs Support
Python Documentation and Books
The Standard Python Documentation Set
The FAQ
Other Published Books
Other Sources of Documentation
Newsgroups, Discussion Groups, and Email Help
comp.lang.python/python-list
comp.lang.python.announce/python-list-announce
[email protected]
The SIGs
JPython-interest
Conferences
Support Companies, Consultants, Training
Tim Peters
B. Platform-Specific Topics
Unix
Unix-Specific Extensions
Standard distribution
SGI-specific extensions
SunOS-specific extensions
Other Unix resources
Windows-Specific Information
Macintosh-Specific Information
Java
Other Platforms
C. Solutions to Exercises
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Index
Colophon
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Cover
Next
Next Chapter
Preface
Learning Python
Mark Lutz
David Ascher
Editor
Frank Willison
Copyright © 1999 O'Reilly Media, Inc.
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset