1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Mark lutz learning python, 5th edition 2013 kho tài liệu bách khoa

1.6K 650 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Table of Contents

  • Preface

    • This Book’s “Ecosystem”

    • About This Fifth Edition

    • The Python 2.X and 3.X Lines

      • The 2.X/3.X Story Today

      • Coverage for Both 3.X and 2.X

      • Which Python Should I Use?

    • This Book’s Prerequisites and Effort

    • This Book’s Structure

    • What This Book Is Not

      • It’s Not a Reference or a Guide to Specific Applications

      • It’s Not the Short Story for People in a Hurry

      • It’s as Linear as Python Allows

    • This Book’s Programs

      • Python Versions

      • Platforms

      • Fetching This Book’s Code

      • Using This Book’s Code

    • Font Conventions

    • Book Updates and Resources

    • Acknowledgments

      • The Backstory

      • Python Thanks

      • Personal Thanks

  • Part I. Getting Started

    • Chapter 1. A Python Q&A Session

      • Why Do People Use Python?

        • Software Quality

        • Developer Productivity

      • Is Python a “Scripting Language”?

      • OK, but What’s the Downside?

      • Who Uses Python Today?

      • What Can I Do with Python?

        • Systems Programming

        • GUIs

        • Internet Scripting

        • Component Integration

        • Database Programming

        • Rapid Prototyping

        • Numeric and Scientific Programming

        • And More: Gaming, Images, Data Mining, Robots, Excel...

      • How Is Python Developed and Supported?

        • Open Source Tradeoffs

      • What Are Python’s Technical Strengths?

        • It’s Object-Oriented and Functional

        • It’s Free

        • It’s Portable

        • It’s Powerful

        • It’s Mixable

        • It’s Relatively Easy to Use

        • It’s Relatively Easy to Learn

        • It’s Named After Monty Python

      • How Does Python Stack Up to Language X?

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 2. How Python Runs Programs

      • Introducing the Python Interpreter

      • Program Execution

        • The Programmer’s View

        • Python’s View

          • Byte code compilation

          • The Python Virtual Machine (PVM)

          • Performance implications

          • Development implications

      • Execution Model Variations

        • Python Implementation Alternatives

          • CPython: The standard

          • Jython: Python for Java

          • IronPython: Python for .NET

          • Stackless: Python for concurrency

          • PyPy: Python for speed

        • Execution Optimization Tools

          • Cython: A Python/C hybrid

          • Shed Skin: A Python-to-C++ translator

          • Psyco: The original just-in-time compiler

        • Frozen Binaries

        • Future Possibilities?

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 3. How You Run Programs

      • The Interactive Prompt

        • Starting an Interactive Session

        • The System Path

        • New Windows Options in 3.3: PATH, Launcher

        • Where to Run: Code Directories

        • What Not to Type: Prompts and Comments

        • Running Code Interactively

        • Why the Interactive Prompt?

          • Experimenting

          • Testing

        • Usage Notes: The Interactive Prompt

          • Entering multiline statements

      • System Command Lines and Files

        • A First Script

        • Running Files with Command Lines

        • Command-Line Usage Variations

        • Usage Notes: Command Lines and Files

      • Unix-Style Executable Scripts: #!

        • Unix Script Basics

        • The Unix env Lookup Trick

        • The Python 3.3 Windows Launcher: #! Comes to Windows

      • Clicking File Icons

        • Icon-Click Basics

        • Clicking Icons on Windows

        • The input Trick on Windows

        • Other Icon-Click Limitations

      • Module Imports and Reloads

        • Import and Reload Basics

        • The Grander Module Story: Attributes

          • Modules and namespaces

        • Usage Notes: import and reload

      • Using exec to Run Module Files

      • The IDLE User Interface

        • IDLE Startup Details

        • IDLE Basic Usage

        • IDLE Usability Features

        • Advanced IDLE Tools

        • Usage Notes: IDLE

      • Other IDEs

      • Other Launch Options

        • Embedding Calls

        • Frozen Binary Executables

        • Text Editor Launch Options

        • Still Other Launch Options

        • Future Possibilities?

      • Which Option Should I Use?

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part I Exercises

  • Part II. Types and Operations

    • Chapter 4. Introducing Python Object Types

      • The Python Conceptual Hierarchy

      • Why Use Built-in Types?

      • Python’s Core Data Types

      • Numbers

      • Strings

        • Sequence Operations

        • Immutability

        • Type-Specific Methods

        • Getting Help

        • Other Ways to Code Strings

        • Unicode Strings

        • Pattern Matching

      • Lists

        • Sequence Operations

        • Type-Specific Operations

        • Bounds Checking

        • Nesting

        • Comprehensions

      • Dictionaries

        • Mapping Operations

        • Nesting Revisited

        • Missing Keys: if Tests

        • Sorting Keys: for Loops

        • Iteration and Optimization

      • Tuples

        • Why Tuples?

      • Files

        • Binary Bytes Files

        • Unicode Text Files

        • Other File-Like Tools

      • Other Core Types

        • How to Break Your Code’s Flexibility

        • User-Defined Classes

        • And Everything Else

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 5. Numeric Types

      • Numeric Type Basics

        • Numeric Literals

        • Built-in Numeric Tools

        • Python Expression Operators

          • Mixed operators follow operator precedence

          • Parentheses group subexpressions

          • Mixed types are converted up

          • Preview: Operator overloading and polymorphism

      • Numbers in Action

        • Variables and Basic Expressions

        • Numeric Display Formats

        • Comparisons: Normal and Chained

        • Division: Classic, Floor, and True

          • Supporting either Python

          • Floor versus truncation

          • Why does truncation matter?

        • Integer Precision

        • Complex Numbers

        • Hex, Octal, Binary: Literals and Conversions

        • Bitwise Operations

        • Other Built-in Numeric Tools

      • Other Numeric Types

        • Decimal Type

          • Decimal basics

          • Setting decimal precision globally

          • Decimal context manager

        • Fraction Type

          • Fraction basics

          • Numeric accuracy in fractions and decimals

          • Fraction conversions and mixed types

        • Sets

          • Set basics in Python 2.6 and earlier

          • Set literals in Python 3.X and 2.7

          • Immutable constraints and frozen sets

          • Set comprehensions in Python 3.X and 2.7

          • Why sets?

        • Booleans

      • Numeric Extensions

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 6. The Dynamic Typing Interlude

      • The Case of the Missing Declaration Statements

        • Variables, Objects, and References

        • Types Live with Objects, Not Variables

        • Objects Are Garbage-Collected

      • Shared References

        • Shared References and In-Place Changes

        • Shared References and Equality

      • Dynamic Typing Is Everywhere

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 7. String Fundamentals

      • This Chapter’s Scope

        • Unicode: The Short Story

      • String Basics

      • String Literals

        • Single- and Double-Quoted Strings Are the Same

        • Escape Sequences Represent Special Characters

        • Raw Strings Suppress Escapes

        • Triple Quotes Code Multiline Block Strings

      • Strings in Action

        • Basic Operations

        • Indexing and Slicing

          • Extended slicing: The third limit and slice objects

        • String Conversion Tools

          • Character code conversions

        • Changing Strings I

      • String Methods

        • Method Call Syntax

        • Methods of Strings

        • String Method Examples: Changing Strings II

        • String Method Examples: Parsing Text

        • Other Common String Methods in Action

        • The Original string Module’s Functions (Gone in 3.X)

      • String Formatting Expressions

        • Formatting Expression Basics

        • Advanced Formatting Expression Syntax

        • Advanced Formatting Expression Examples

        • Dictionary-Based Formatting Expressions

      • String Formatting Method Calls

        • Formatting Method Basics

        • Adding Keys, Attributes, and Offsets

        • Advanced Formatting Method Syntax

        • Advanced Formatting Method Examples

        • Comparison to the % Formatting Expression

        • Why the Format Method?

          • Extra features: Special-case “batteries” versus general techniques

          • Flexible reference syntax: Extra complexity and functional overlap

          • Explicit value references: Now optional and unlikely to be used

          • Named method and context-neutral arguments: Aesthetics versus practice

          • Functions versus expressions: A minor convenience

      • General Type Categories

        • Types Share Operation Sets by Categories

        • Mutable Types Can Be Changed in Place

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 8. Lists and Dictionaries

      • Lists

      • Lists in Action

        • Basic List Operations

        • List Iteration and Comprehensions

        • Indexing, Slicing, and Matrixes

        • Changing Lists in Place

          • Index and slice assignments

          • List method calls

          • More on sorting lists

          • Other common list methods

          • Other common list operations

      • Dictionaries

      • Dictionaries in Action

        • Basic Dictionary Operations

        • Changing Dictionaries in Place

        • More Dictionary Methods

        • Example: Movie Database

          • Preview: Mapping values to keys

        • Dictionary Usage Notes

          • Using dictionaries to simulate flexible lists: Integer keys

          • Using dictionaries for sparse data structures: Tuple keys

          • Avoiding missing-key errors

          • Nesting in dictionaries

        • Other Ways to Make Dictionaries

        • Dictionary Changes in Python 3.X and 2.7

          • Dictionary comprehensions in 3.X and 2.7

          • Dictionary views in 3.X (and 2.7 via new methods)

          • Dictionary views and sets

          • Sorting dictionary keys in 3.X

          • Dictionary magnitude comparisons no longer work in 3.X

          • The has_key method is dead in 3.X: Long live in!

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 9. Tuples, Files, and Everything Else

      • Tuples

        • Tuples in Action

          • Tuple syntax peculiarities: Commas and parentheses

          • Conversions, methods, and immutability

        • Why Lists and Tuples?

        • Records Revisited: Named Tuples

      • Files

        • Opening Files

        • Using Files

        • Files in Action

        • Text and Binary Files: The Short Story

        • Storing Python Objects in Files: Conversions

        • Storing Native Python Objects: pickle

        • Storing Python Objects in JSON Format

        • Storing Packed Binary Data: struct

        • File Context Managers

        • Other File Tools

      • Core Types Review and Summary

        • Object Flexibility

        • References Versus Copies

        • Comparisons, Equality, and Truth

          • Python 2.X and 3.X mixed-type comparisons and sorts

          • Python 2.X and 3.X dictionary comparisons

        • The Meaning of True and False in Python

          • The None object

          • The bool type

        • Python’s Type Hierarchies

        • Type Objects

        • Other Types in Python

      • Built-in Type Gotchas

        • Assignment Creates References, Not Copies

        • Repetition Adds One Level Deep

        • Beware of Cyclic Data Structures

        • Immutable Types Can’t Be Changed in Place

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part II Exercises

  • Part III. Statements and Syntax

    • Chapter 10. Introducing Python Statements

      • The Python Conceptual Hierarchy Revisited

      • Python’s Statements

      • A Tale of Two ifs

        • What Python Adds

        • What Python Removes

          • Parentheses are optional

          • End-of-line is end of statement

          • End of indentation is end of block

        • Why Indentation Syntax?

        • A Few Special Cases

          • Statement rule special cases

          • Block rule special case

      • A Quick Example: Interactive Loops

        • A Simple Interactive Loop

        • Doing Math on User Inputs

        • Handling Errors by Testing Inputs

        • Handling Errors with try Statements

          • Supporting floating-point numbers

        • Nesting Code Three Levels Deep

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 11. Assignments, Expressions, and Prints

      • Assignment Statements

        • Assignment Statement Forms

        • Sequence Assignments

          • Advanced sequence assignment patterns

        • Extended Sequence Unpacking in Python 3.X

          • Extended unpacking in action

          • Boundary cases

          • A useful convenience

          • Application to for loops

        • Multiple-Target Assignments

          • Multiple-target assignment and shared references

        • Augmented Assignments

          • Augmented assignment and shared references

        • Variable Name Rules

          • Naming conventions

          • Names have no type, but objects do

      • Expression Statements

        • Expression Statements and In-Place Changes

      • Print Operations

        • The Python 3.X print Function

          • Call format

          • The 3.X print function in action

        • The Python 2.X print Statement

          • Statement forms

          • The 2.X print statement in action

        • Print Stream Redirection

          • The Python “hello world” program

          • Manual stream redirection

          • Automatic stream redirection

        • Version-Neutral Printing

          • 2to3 converter

          • Importing from __future__

          • Neutralizing display differences with code

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 12. if Tests and Syntax Rules

      • if Statements

        • General Format

        • Basic Examples

        • Multiway Branching

          • Handling switch defaults

          • Handling larger actions

      • Python Syntax Revisited

        • Block Delimiters: Indentation Rules

          • Avoid mixing tabs and spaces: New error checking in 3.X

        • Statement Delimiters: Lines and Continuations

        • A Few Special Cases

      • Truth Values and Boolean Tests

      • The if/else Ternary Expression

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 13. while and for Loops

      • while Loops

        • General Format

        • Examples

      • break, continue, pass, and the Loop else

        • General Loop Format

        • pass

        • continue

        • break

        • Loop else

          • More on the loop else

      • for Loops

        • General Format

        • Examples

          • Basic usage

          • Other data types

          • Tuple assignment in for loops

          • Python 3.X extended sequence assignment in for loops

          • Nested for loops

      • Loop Coding Techniques

        • Counter Loops: range

        • Sequence Scans: while and range Versus for

        • Sequence Shufflers: range and len

        • Nonexhaustive Traversals: range Versus Slices

        • Changing Lists: range Versus Comprehensions

        • Parallel Traversals: zip and map

          • map equivalence in Python 2.X

          • Dictionary construction with zip

        • Generating Both Offsets and Items: enumerate

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 14. Iterations and Comprehensions

      • Iterations: A First Look

        • The Iteration Protocol: File Iterators

        • Manual Iteration: iter and next

          • The full iteration protocol

          • Manual iteration

        • Other Built-in Type Iterables

      • List Comprehensions: A First Detailed Look

        • List Comprehension Basics

        • Using List Comprehensions on Files

        • Extended List Comprehension Syntax

          • Filter clauses: if

          • Nested loops: for

      • Other Iteration Contexts

      • New Iterables in Python 3.X

        • Impacts on 2.X Code: Pros and Cons

        • The range Iterable

        • The map, zip, and filter Iterables

        • Multiple Versus Single Pass Iterators

        • Dictionary View Iterables

      • Other Iteration Topics

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 15. The Documentation Interlude

      • Python Documentation Sources

        • # Comments

        • The dir Function

        • Docstrings: __doc__

          • User-defined docstrings

          • Docstring standards and priorities

          • Built-in docstrings

        • PyDoc: The help Function

        • PyDoc: HTML Reports

          • Python 3.2 and later: PyDoc’s all-browser mode

          • Python 3.2 and earlier: GUI client

        • Beyond docstrings: Sphinx

        • The Standard Manual Set

        • Web Resources

        • Published Books

      • Common Coding Gotchas

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part III Exercises

  • Part IV. Functions and Generators

    • Chapter 16. Function Basics

      • Why Use Functions?

      • Coding Functions

        • def Statements

        • def Executes at Runtime

      • A First Example: Definitions and Calls

        • Definition

        • Calls

        • Polymorphism in Python

      • A Second Example: Intersecting Sequences

        • Definition

        • Calls

        • Polymorphism Revisited

        • Local Variables

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 17. Scopes

      • Python Scope Basics

        • Scope Details

        • Name Resolution: The LEGB Rule

          • Other Python scopes: Preview

        • Scope Example

        • The Built-in Scope

          • Redefining built-in names: For better or worse

      • The global Statement

        • Program Design: Minimize Global Variables

        • Program Design: Minimize Cross-File Changes

        • Other Ways to Access Globals

      • Scopes and Nested Functions

        • Nested Scope Details

        • Nested Scope Examples

        • Factory Functions: Closures

          • A simple function factory

          • Closures versus classes, round 1

        • Retaining Enclosing Scope State with Defaults

          • Nested scopes, defaults, and lambdas

          • Loop variables may require defaults, not scopes

          • Arbitrary scope nesting

      • The nonlocal Statement in 3.X

        • nonlocal Basics

        • nonlocal in Action

          • Using nonlocal for changes

          • Boundary cases

      • Why nonlocal? State Retention Options

        • State with nonlocal: 3.X only

        • State with Globals: A Single Copy Only

        • State with Classes: Explicit Attributes (Preview)

        • State with Function Attributes: 3.X and 2.X

          • State with mutables: Obscure ghost of Pythons past?

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 18. Arguments

      • Argument-Passing Basics

        • Arguments and Shared References

        • Avoiding Mutable Argument Changes

        • Simulating Output Parameters and Multiple Results

      • Special Argument-Matching Modes

        • Argument Matching Basics

        • Argument Matching Syntax

        • The Gritty Details

        • Keyword and Default Examples

          • Keywords

          • Defaults

          • Combining keywords and defaults

        • Arbitrary Arguments Examples

          • Headers: Collecting arguments

          • Calls: Unpacking arguments

          • Applying functions generically

          • The defunct apply built-in (Python 2.X)

        • Python 3.X Keyword-Only Arguments

          • Ordering rules

          • Why keyword-only arguments?

      • The min Wakeup Call!

        • Full Credit

        • Bonus Points

        • The Punch Line...

      • Generalized Set Functions

      • Emulating the Python 3.X print Function

        • Using Keyword-Only Arguments

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 19. Advanced Function Topics

      • Function Design Concepts

      • Recursive Functions

        • Summation with Recursion

        • Coding Alternatives

        • Loop Statements Versus Recursion

        • Handling Arbitrary Structures

          • Recursion versus queues and stacks

          • Cycles, paths, and stack limits

          • More recursion examples

      • Function Objects: Attributes and Annotations

        • Indirect Function Calls: “First Class” Objects

        • Function Introspection

        • Function Attributes

        • Function Annotations in 3.X

      • Anonymous Functions: lambda

        • lambda Basics

        • Why Use lambda?

          • Multiway branch switches: The finale

        • How (Not) to Obfuscate Your Python Code

        • Scopes: lambdas Can Be Nested Too

      • Functional Programming Tools

        • Mapping Functions over Iterables: map

        • Selecting Items in Iterables: filter

        • Combining Items in Iterables: reduce

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 20. Comprehensions and Generations

      • List Comprehensions and Functional Tools

        • List Comprehensions Versus map

        • Adding Tests and Nested Loops: filter

          • Formal comprehension syntax

        • Example: List Comprehensions and Matrixes

        • Don’t Abuse List Comprehensions: KISS

          • On the other hand: performance, conciseness, expressiveness

      • Generator Functions and Expressions

        • Generator Functions: yield Versus return

          • State suspension

          • Iteration protocol integration

          • Generator functions in action

          • Why generator functions?

          • Extended generator function protocol: send versus next

        • Generator Expressions: Iterables Meet Comprehensions

          • Why generator expressions?

          • Generator expressions versus map

          • Generator expressions versus filter

        • Generator Functions Versus Generator Expressions

        • Generators Are Single-Iteration Objects

        • Generation in Built-in Types, Tools, and Classes

          • Generators and library tools: Directory walkers

          • Generators and function application

          • Preview: User-defined iterables in classes

        • Example: Generating Scrambled Sequences

          • Scrambling sequences

          • Simple functions

          • Generator functions

          • Generator expressions

          • Tester client

          • Permutations: All possible combinations

        • Don’t Abuse Generators: EIBTI

          • On the other hand: Space and time, conciseness, expressiveness

        • Example: Emulating zip and map with Iteration Tools

          • Coding your own map(func, ...)

          • Coding your own zip(...) and map(None, ...)

      • Comprehension Syntax Summary

        • Scopes and Comprehension Variables

        • Comprehending Set and Dictionary Comprehensions

        • Extended Comprehension Syntax for Sets and Dictionaries

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 21. The Benchmarking Interlude

      • Timing Iteration Alternatives

        • Timing Module: Homegrown

        • Timing Script

        • Timing Results

          • The impact of function calls: map

        • Timing Module Alternatives

          • Using keyword-only arguments in 3.X

        • Other Suggestions

      • Timing Iterations and Pythons with timeit

        • Basic timeit Usage

          • Interactive usage and API calls

          • Command-line usage

          • Timing multiline statements

          • Other usage modes: Setup, totals, and objects

        • Benchmark Module and Script: timeit

        • Benchmark Script Results

        • More Fun with Benchmarks

          • A win for map and a rare loss for PyPy

          • The impact of function calls revisited

          • Comparing techniques: Homegrown versus batteries

          • Room for improvement: Setup

      • Other Benchmarking Topics: pystones

      • Function Gotchas

        • Local Names Are Detected Statically

        • Defaults and Mutable Objects

        • Functions Without returns

        • Miscellaneous Function Gotchas

          • Enclosing scopes and loop variables: Factory functions

          • Hiding built-ins by assignment: Shadowing

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part IV Exercises

  • Part V. Modules and Packages

    • Chapter 22. Modules: The Big Picture

      • Why Use Modules?

      • Python Program Architecture

        • How to Structure a Program

        • Imports and Attributes

        • Standard Library Modules

      • How Imports Work

        • 1. Find It

        • 2. Compile It (Maybe)

        • 3. Run It

      • Byte Code Files: __pycache__ in Python 3.2+

        • Byte Code File Models in Action

      • The Module Search Path

        • Configuring the Search Path

        • Search Path Variations

        • The sys.path List

        • Module File Selection

          • Module sources

          • Selection priorities

          • Import hooks and ZIP files

          • Optimized byte code files

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 23. Module Coding Basics

      • Module Creation

        • Module Filenames

        • Other Kinds of Modules

      • Module Usage

        • The import Statement

        • The from Statement

        • The from * Statement

        • Imports Happen Only Once

          • Initialization code

        • import and from Are Assignments

          • Changing mutables in modules

          • Cross-file name changes

        • import and from Equivalence

        • Potential Pitfalls of the from Statement

          • When import is required

      • Module Namespaces

        • Files Generate Namespaces

        • Namespace Dictionaries: __dict__

        • Attribute Name Qualification

        • Imports Versus Scopes

        • Namespace Nesting

      • Reloading Modules

        • reload Basics

        • reload Example

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 24. Module Packages

      • Package Import Basics

        • Packages and Search Path Settings

        • Package __init__.py Files

          • Package initialization file roles

      • Package Import Example

        • from Versus import with Packages

      • Why Use Package Imports?

        • A Tale of Three Systems

      • Package Relative Imports

        • Changes in Python 3.X

        • Relative Import Basics

        • Why Relative Imports?

          • The relative imports solution in 3.X

          • Relative imports versus absolute package paths

        • The Scope of Relative Imports

        • Module Lookup Rules Summary

        • Relative Imports in Action

          • Imports outside packages

          • Imports within packages

          • Imports are still relative to the CWD

          • Selecting modules with relative and absolute imports

          • Relative imports search packages only

          • Imports are still relative to the CWD, again

        • Pitfalls of Package-Relative Imports: Mixed Use

          • The issue

          • Fix 1: Package subdirectories

          • Fix 2: Full path absolute import

          • Example: Application to module self-test code (preview)

      • Python 3.3 Namespace Packages

        • Namespace Package Semantics

          • The import algorithm

        • Impacts on Regular Packages: Optional __init__.py

        • Namespace Packages in Action

        • Namespace Package Nesting

        • Files Still Have Precedence over Directories

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 25. Advanced Module Topics

      • Module Design Concepts

      • Data Hiding in Modules

        • Minimizing from * Damage: _X and __all__

      • Enabling Future Language Features: __future__

      • Mixed Usage Modes: __name__ and __main__

        • Unit Tests with __name__

      • Example: Dual Mode Code

        • Currency Symbols: Unicode in Action

        • Docstrings: Module Documentation at Work

      • Changing the Module Search Path

      • The as Extension for import and from

      • Example: Modules Are Objects

      • Importing Modules by Name String

        • Running Code Strings

        • Direct Calls: Two Options

      • Example: Transitive Module Reloads

        • A Recursive Reloader

          • Testing recursive reloads

        • Alternative Codings

          • Testing reload variants

      • Module Gotchas

        • Module Name Clashes: Package and Package-Relative Imports

        • Statement Order Matters in Top-Level Code

        • from Copies Names but Doesn’t Link

        • from * Can Obscure the Meaning of Variables

        • reload May Not Impact from Imports

        • reload, from, and Interactive Testing

        • Recursive from Imports May Not Work

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part V Exercises

  • Part VI. Classes and OOP

    • Chapter 26. OOP: The Big Picture

      • Why Use Classes?

      • OOP from 30,000 Feet

        • Attribute Inheritance Search

        • Classes and Instances

        • Method Calls

        • Coding Class Trees

        • Operator Overloading

        • OOP Is About Code Reuse

          • Polymorphism and classes

          • Programming by customization

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 27. Class Coding Basics

      • Classes Generate Multiple Instance Objects

        • Class Objects Provide Default Behavior

        • Instance Objects Are Concrete Items

        • A First Example

      • Classes Are Customized by Inheritance

        • A Second Example

        • Classes Are Attributes in Modules

      • Classes Can Intercept Python Operators

        • A Third Example

          • Returning results, or not

        • Why Use Operator Overloading?

      • The World’s Simplest Python Class

        • Records Revisited: Classes Versus Dictionaries

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 28. A More Realistic Example

      • Step 1: Making Instances

        • Coding Constructors

        • Testing As You Go

        • Using Code Two Ways

      • Step 2: Adding Behavior Methods

        • Coding Methods

      • Step 3: Operator Overloading

        • Providing Print Displays

      • Step 4: Customizing Behavior by Subclassing

        • Coding Subclasses

        • Augmenting Methods: The Bad Way

        • Augmenting Methods: The Good Way

        • Polymorphism in Action

        • Inherit, Customize, and Extend

        • OOP: The Big Idea

      • Step 5: Customizing Constructors, Too

        • OOP Is Simpler Than You May Think

        • Other Ways to Combine Classes

      • Step 6: Using Introspection Tools

        • Special Class Attributes

        • A Generic Display Tool

        • Instance Versus Class Attributes

        • Name Considerations in Tool Classes

        • Our Classes’ Final Form

      • Step 7 (Final): Storing Objects in a Database

        • Pickles and Shelves

          • The pickle module

          • The shelve module

        • Storing Objects on a Shelve Database

        • Exploring Shelves Interactively

        • Updating Objects on a Shelve

      • Future Directions

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 29. Class Coding Details

      • The class Statement

        • General Form

        • Example

      • Methods

        • Method Example

        • Calling Superclass Constructors

        • Other Method Call Possibilities

      • Inheritance

        • Attribute Tree Construction

        • Specializing Inherited Methods

        • Class Interface Techniques

        • Abstract Superclasses

          • Abstract superclasses in Python 3.X and 2.6+: Preview

      • Namespaces: The Conclusion

        • Simple Names: Global Unless Assigned

        • Attribute Names: Object Namespaces

        • The “Zen” of Namespaces: Assignments Classify Names

        • Nested Classes: The LEGB Scopes Rule Revisited

        • Namespace Dictionaries: Review

        • Namespace Links: A Tree Climber

      • Documentation Strings Revisited

      • Classes Versus Modules

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 30. Operator Overloading

      • The Basics

        • Constructors and Expressions: __init__ and __sub__

        • Common Operator Overloading Methods

      • Indexing and Slicing: __getitem__ and __setitem__

        • Intercepting Slices

        • Slicing and Indexing in Python 2.X

        • But 3.X’s __index__ Is Not Indexing!

      • Index Iteration: __getitem__

      • Iterable Objects: __iter__ and __next__

        • User-Defined Iterables

          • Single versus multiple scans

          • Classes versus generators

        • Multiple Iterators on One Object

          • Classes versus slices

        • Coding Alternative: __iter__ plus yield

          • Multiple iterators with yield

      • Membership: __contains__, __iter__, and __getitem__

      • Attribute Access: __getattr__ and __setattr__

        • Attribute Reference

        • Attribute Assignment and Deletion

        • Other Attribute Management Tools

        • Emulating Privacy for Instance Attributes: Part 1

      • String Representation: __repr__ and __str__

        • Why Two Display Methods?

        • Display Usage Notes

      • Right-Side and In-Place Uses: __radd__ and __iadd__

        • Right-Side Addition

          • Reusing __add__ in __radd__

          • Propagating class type

        • In-Place Addition

      • Call Expressions: __call__

        • Function Interfaces and Callback-Based Code

      • Comparisons: __lt__, __gt__, and Others

        • The __cmp__ Method in Python 2.X

      • Boolean Tests: __bool__ and __len__

        • Boolean Methods in Python 2.X

      • Object Destruction: __del__

        • Destructor Usage Notes

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 31. Designing with Classes

      • Python and OOP

        • Polymorphism Means Interfaces, Not Call Signatures

      • OOP and Inheritance: “Is-a” Relationships

      • OOP and Composition: “Has-a” Relationships

        • Stream Processors Revisited

      • OOP and Delegation: “Wrapper” Proxy Objects

      • Pseudoprivate Class Attributes

        • Name Mangling Overview

        • Why Use Pseudoprivate Attributes?

      • Methods Are Objects: Bound or Unbound

        • Unbound Methods Are Functions in 3.X

        • Bound Methods and Other Callable Objects

          • Other callables

      • Classes Are Objects: Generic Object Factories

        • Why Factories?

      • Multiple Inheritance: “Mix-in” Classes

        • Coding Mix-in Display Classes

          • Listing instance attributes with __dict__

          • Listing inherited attributes with dir

          • Listing attributes per object in class trees

          • Running the tree lister

          • Usage variation: Showing underscore name values

          • Usage variation: Running on larger modules

          • Collector module

          • Room for improvement: MRO, slots, GUIs

      • Other Design-Related Topics

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 32. Advanced Class Topics

      • Extending Built-in Types

        • Extending Types by Embedding

        • Extending Types by Subclassing

      • The “New Style” Class Model

        • Just How New Is New-Style?

      • New-Style Class Changes

        • Attribute Fetch for Built-ins Skips Instances

          • Why the lookup change?

          • Implications for attribute interception

          • Proxy coding requirements

          • For more details

        • Type Model Changes

          • Implications for type testing

        • All Classes Derive from “object”

          • Implications for defaults

        • Diamond Inheritance Change

          • Implications for diamond inheritance trees

          • Explicit conflict resolution

          • Scope of search order change

        • More on the MRO: Method Resolution Order

          • The MRO algorithm

          • Tracing the MRO

        • Example: Mapping Attributes to Inheritance Sources

      • New-Style Class Extensions

        • Slots: Attribute Declarations

          • Slot basics

          • Slots and namespace dictionaries

          • Multiple __slot__ lists in superclasses

          • Handling slots and other “virtual” attributes generically

          • Slot usage rules

          • Example impacts of slots: ListTree and mapattrs

          • What about slots speed?

        • Properties: Attribute Accessors

          • Property basics

        • __getattribute__ and Descriptors: Attribute Tools

        • Other Class Changes and Extensions

      • Static and Class Methods

        • Why the Special Methods?

        • Static Methods in 2.X and 3.X

        • Static Method Alternatives

        • Using Static and Class Methods

        • Counting Instances with Static Methods

        • Counting Instances with Class Methods

          • Counting instances per class with class methods

      • Decorators and Metaclasses: Part 1

        • Function Decorator Basics

        • A First Look at User-Defined Function Decorators

        • A First Look at Class Decorators and Metaclasses

        • For More Details

      • The super Built-in Function: For Better or Worse?

        • The Great super Debate

        • Traditional Superclass Call Form: Portable, General

        • Basic super Usage and Its Tradeoffs

          • Odd semantics: A magic proxy in Python 3.X

          • Pitfall: Adding multiple inheritance naively

          • Limitation: Operator overloading

          • Use differs in Python 2.X: Verbose calls

        • The super Upsides: Tree Changes and Dispatch

        • Runtime Class Changes and super

        • Cooperative Multiple Inheritance Method Dispatch

          • The basics: Cooperative super call in action

          • Constraint: Call chain anchor requirement

          • Scope: An all-or-nothing model

          • Flexibility: Call ordering assumptions

          • Customization: Method replacement

          • Coupling: Application to mix-in classes

          • Customization: Same-argument constraints

        • The super Summary

      • Class Gotchas

        • Changing Class Attributes Can Have Side Effects

        • Changing Mutable Class Attributes Can Have Side Effects, Too

        • Multiple Inheritance: Order Matters

        • Scopes in Methods and Classes

        • Miscellaneous Class Gotchas

          • Choose per-instance or class storage wisely

          • You usually want to call superclass constructors

          • Delegation-based classes in 3.X: __getattr__ and built-ins

        • KISS Revisited: “Overwrapping-itis”

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part VI Exercises

  • Part VII. Exceptions and Tools

    • Chapter 33. Exception Basics

      • Why Use Exceptions?

        • Exception Roles

      • Exceptions: The Short Story

        • Default Exception Handler

        • Catching Exceptions

        • Raising Exceptions

        • User-Defined Exceptions

        • Termination Actions

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 34. Exception Coding Details

      • The try/except/else Statement

        • How try Statements Work

        • try Statement Clauses

          • Catching any and all exceptions

          • Catching all: The empty except and Exception

        • The try else Clause

        • Example: Default Behavior

        • Example: Catching Built-in Exceptions

      • The try/finally Statement

        • Example: Coding Termination Actions with try/finally

      • Unified try/except/finally

        • Unified try Statement Syntax

        • Combining finally and except by Nesting

        • Unified try Example

      • The raise Statement

        • Raising Exceptions

        • Scopes and try except Variables

        • Propagating Exceptions with raise

        • Python 3.X Exception Chaining: raise from

      • The assert Statement

        • Example: Trapping Constraints (but Not Errors!)

      • with/as Context Managers

        • Basic Usage

        • The Context Management Protocol

        • Multiple Context Managers in 3.1, 2.7, and Later

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 35. Exception Objects

      • Exceptions: Back to the Future

        • String Exceptions Are Right Out!

        • Class-Based Exceptions

        • Coding Exceptions Classes

      • Why Exception Hierarchies?

      • Built-in Exception Classes

        • Built-in Exception Categories

        • Default Printing and State

      • Custom Print Displays

      • Custom Data and Behavior

        • Providing Exception Details

        • Providing Exception Methods

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 36. Designing with Exceptions

      • Nesting Exception Handlers

        • Example: Control-Flow Nesting

        • Example: Syntactic Nesting

      • Exception Idioms

        • Breaking Out of Multiple Nested Loops: “go to”

        • Exceptions Aren’t Always Errors

        • Functions Can Signal Conditions with raise

        • Closing Files and Server Connections

        • Debugging with Outer try Statements

        • Running In-Process Tests

        • More on sys.exc_info

        • Displaying Errors and Tracebacks

      • Exception Design Tips and Gotchas

        • What Should Be Wrapped

        • Catching Too Much: Avoid Empty except and Exception

        • Catching Too Little: Use Class-Based Categories

      • Core Language Summary

        • The Python Toolset

        • Development Tools for Larger Projects

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part VII Exercises

  • Part VIII. Advanced Topics

    • Chapter 37. Unicode and Byte Strings

      • String Changes in 3.X

      • String Basics

        • Character Encoding Schemes

        • How Python Stores Strings in Memory

        • Python’s String Types

          • Why the different string types?

        • Text and Binary Files

      • Coding Basic Strings

        • Python 3.X String Literals

          • Python 2.X Unicode literals in Python 3.3

        • Python 2.X String Literals

        • String Type Conversions

      • Coding Unicode Strings

        • Coding ASCII Text

        • Coding Non-ASCII Text

        • Encoding and Decoding Non-ASCII text

        • Other Encoding Schemes

        • Byte String Literals: Encoded Text

        • Converting Encodings

        • Coding Unicode Strings in Python 2.X

          • Mixing string types in 2.X

        • Source File Character Set Encoding Declarations

      • Using 3.X bytes Objects

        • Method Calls

        • Sequence Operations

        • Other Ways to Make bytes Objects

        • Mixing String Types

      • Using 3.X/2.6+ bytearray Objects

        • bytearrays in Action

        • Python 3.X String Types Summary

      • Using Text and Binary Files

        • Text File Basics

        • Text and Binary Modes in 2.X and 3.X

        • Type and Content Mismatches in 3.X

      • Using Unicode Files

        • Reading and Writing Unicode in 3.X

          • Manual encoding

          • File output encoding

          • File input decoding

          • Decoding mismatches

        • Handling the BOM in 3.X

          • Dropping the BOM in Notepad

          • Dropping the BOM in Python

        • Unicode Files in 2.X

        • Unicode Filenames and Streams

          • Filenames: Text versus bytes

          • Stream content: PYTHONIOENCODING

      • Other String Tool Changes in 3.X

        • The re Pattern-Matching Module

        • The struct Binary Data Module

        • The pickle Object Serialization Module

        • XML Parsing Tools

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 38. Managed Attributes

      • Why Manage Attributes?

        • Inserting Code to Run on Attribute Access

      • Properties

        • The Basics

        • A First Example

        • Computed Attributes

        • Coding Properties with Decorators

          • Setter and deleter decorators

      • Descriptors

        • The Basics

          • Descriptor method arguments

          • Read-only descriptors

        • A First Example

        • Computed Attributes

        • Using State Information in Descriptors

        • How Properties and Descriptors Relate

          • Descriptors and slots and more

      • __getattr__ and __getattribute__

        • The Basics

          • Avoiding loops in attribute interception methods

        • A First Example

          • Using __getattribute__

        • Computed Attributes

          • Using __getattribute__

        • __getattr__ and __getattribute__ Compared

        • Management Techniques Compared

        • Intercepting Built-in Operation Attributes

          • Delegation-based managers revisited

      • Example: Attribute Validations

        • Using Properties to Validate

          • Testing code

        • Using Descriptors to Validate

          • Option 1: Validating with shared descriptor instance state

          • Option 2: Validating with per-client-instance state

        • Using __getattr__ to Validate

        • Using __getattribute__ to Validate

      • Chapter Summary

      • Test Your Knowledge: Quiz

        • Test Your Knowledge: Answers

    • Chapter 39. Decorators

      • What’s a Decorator?

        • Managing Calls and Instances

        • Managing Functions and Classes

        • Using and Defining Decorators

        • Why Decorators?

      • The Basics

        • Function Decorators

          • Usage

          • Implementation

          • Supporting method decoration

        • Class Decorators

          • Usage

          • Implementation

          • Supporting multiple instances

        • Decorator Nesting

        • Decorator Arguments

        • Decorators Manage Functions and Classes, Too

      • Coding Function Decorators

        • Tracing Calls

        • Decorator State Retention Options

          • Class instance attributes

          • Enclosing scopes and globals

          • Enclosing scopes and nonlocals

          • Function attributes

        • Class Blunders I: Decorating Methods

          • Using nested functions to decorate methods

          • Using descriptors to decorate methods

        • Timing Calls

          • Decorators versus per-call timing

          • Testing subtleties

        • Adding Decorator Arguments

          • Timing with decorator arguments

      • Coding Class Decorators

        • Singleton Classes

          • Coding alternatives

        • Tracing Object Interfaces

          • Tracing interfaces with class decorators

          • Applying class decorators to built-in types

        • Class Blunders II: Retaining Multiple Instances

        • Decorators Versus Manager Functions

        • Why Decorators? (Revisited)

      • Managing Functions and Classes Directly

      • Example: “Private” and “Public” Attributes

        • Implementing Private Attributes

        • Implementation Details I

          • Inheritance versus delegation

          • Decorator arguments

          • State retention and enclosing scopes

          • Using __dict__ and __slots__ (and other virtual names)

        • Generalizing for Public Declarations, Too

        • Implementation Details II

          • Using __X pseudoprivate names

          • Breaking privacy

          • Decorator tradeoffs

        • Open Issues

          • Caveat: Implicitly run operator overloading methods fail to delegate under 3.X

          • Approaches to redefining operator overloading methods for 3.X

            • Inline definition

            • Mix-in superclasses

            • Coding variations: Routers, descriptors, automation

          • Should operator methods be validated?

          • Implementation alternatives: __getattribute__ inserts, call stack inspection

        • Python Isn’t About Control

      • Example: Validating Function Arguments

        • The Goal

        • A Basic Range-Testing Decorator for Positional Arguments

        • Generalizing for Keywords and Defaults, Too

        • Implementation Details

          • Function introspection

          • Argument assumptions

          • Matching algorithm

        • Open Issues

          • Invalid calls

          • Arbitrary arguments

          • Decorator nesting

        • Decorator Arguments Versus Function Annotations

        • Other Applications: Type Testing (If You Insist!)

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 40. Metaclasses

      • To Metaclass or Not to Metaclass

        • Increasing Levels of “Magic”

        • A Language of Hooks

        • The Downside of “Helper” Functions

        • Metaclasses Versus Class Decorators: Round 1

      • The Metaclass Model

        • Classes Are Instances of type

        • Metaclasses Are Subclasses of Type

        • Class Statement Protocol

      • Declaring Metaclasses

        • Declaration in 3.X

        • Declaration in 2.X

        • Metaclass Dispatch in Both 3.X and 2.X

      • Coding Metaclasses

        • A Basic Metaclass

        • Customizing Construction and Initialization

        • Other Metaclass Coding Techniques

          • Using simple factory functions

          • Overloading class creation calls with normal classes

          • Overloading class creation calls with metaclasses

      • Inheritance and Instance

        • Metaclass Versus Superclass

        • Inheritance: The Full Story

          • Python’s inheritance algorithm: The simple version

          • The descriptors special case

          • Python’s inheritance algorithm: The somewhat-more-complete version

          • Assignment inheritance

          • The built-ins special case

      • Metaclass Methods

        • Metaclass Methods Versus Class Methods

        • Operator Overloading in Metaclass Methods

      • Example: Adding Methods to Classes

        • Manual Augmentation

        • Metaclass-Based Augmentation

        • Metaclasses Versus Class Decorators: Round 2

          • Decorator-based augmentation

          • Managing instances instead of classes

          • Metaclass and class decorator equivalence?

      • Example: Applying Decorators to Methods

        • Tracing with Decoration Manually

        • Tracing with Metaclasses and Decorators

        • Applying Any Decorator to Methods

        • Metaclasses Versus Class Decorators: Round 3 (and Last)

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 41. All Good Things

      • The Python Paradox

        • On “Optional” Language Features

        • Against Disquieting Improvements

        • Complexity Versus Power

        • Simplicity Versus Elitism

        • Closing Thoughts

      • Where to Go From Here

      • Encore: Print Your Own Completion Certificate!

  • Part IX. Appendixes

    • Appendix A. Installation and Configuration

      • Installing the Python Interpreter

        • Is Python Already Present?

        • Where to Get Python

        • Installation Steps

      • Configuring Python

        • Python Environment Variables

        • How to Set Configuration Options

          • Unix/Linux shell variables

          • DOS variables (and older Windows)

          • Windows environment variable GUI

          • Windows registry

          • Path files

        • Python Command-Line Arguments

          • Running script files with arguments

          • Running code given in arguments and standard input

          • Running modules on the search path

          • Optimized and unbuffered modes

          • Post-run interactive mode

          • Python 2.X command-line arguments

        • Python 3.3 Windows Launcher Command Lines

      • For More Help

    • Appendix B. The Python 3.3 Windows Launcher

      • The Unix Legacy

      • The Windows Legacy

      • Introducing the New Windows Launcher

      • A Windows Launcher Tutorial

        • Step 1: Using Version Directives in Files

        • Step 2: Using Command-Line Version Switches

        • Step 3: Using and Changing Defaults

      • Pitfalls of the New Windows Launcher

        • Pitfall 1: Unrecognized Unix !# Lines Fail

          • Book examples impact and fix

        • Pitfall 2: The Launcher Defaults to 2.X

          • Book examples impact and fix

        • Pitfall 3: The New PATH Extension Option

      • Conclusions: A Net Win for Windows

    • Appendix C. Python Changes and This Book

      • Major 2.X/3.X Differences

        • 3.X Differences

        • 3.X-Only Extensions

      • General Remarks: 3.X Changes

        • Changes in Libraries and Tools

          • Standard library changes

          • Tools changes

        • Migrating to 3.X

      • Fifth Edition Python Changes: 2.7, 3.2, 3.3

        • Changes in Python 2.7

        • Changes in Python 3.3

        • Changes in Python 3.2

      • Fourth Edition Python Changes: 2.6, 3.0, 3.1

        • Changes in Python 3.1

        • Changes in Python 3.0 and 2.6

        • Specific Language Removals in 3.0

      • Third Edition Python Changes: 2.3, 2.4, 2.5

      • Earlier and Later Python Changes

    • Appendix D. Solutions to End-of-Part Exercises

      • Part I, Getting Started

      • Part II, Types and Operations

      • Part III, Statements and Syntax

      • Part IV, Functions and Generators

      • Part V, Modules and Packages

      • Part VI, Classes and OOP

      • Part VII, Exceptions and Tools

  • Index

Nội dung

FIFTH EDITION Download from Wow! eBook Learning Python Mark Lutz Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo Learning Python, Fifth Edition by Mark Lutz Copyright © 2013 Mark Lutz All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://my.safaribooksonline.com) For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com Editor: Rachel Roumeliotis Production Editor: Christopher Hearse Copyeditor: Rachel Monaghan Proofreader: Julie Van Keuren June 2013: Indexer: Lucie Haskins Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest Fifth Edition Revision History for the Fifth Edition: 2013-06-07 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449355739 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Learning Python, 5th Edition, the image of a wood rat, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-1-449-35573-9 [QG] 1370970520 To Vera You are my life Table of Contents Preface xxxiii Part I Getting Started A Python Q&A Session Why Do People Use Python? Software Quality Developer Productivity Is Python a “Scripting Language”? OK, but What’s the Downside? Who Uses Python Today? What Can I Do with Python? Systems Programming GUIs Internet Scripting Component Integration Database Programming Rapid Prototyping Numeric and Scientific Programming And More: Gaming, Images, Data Mining, Robots, Excel How Is Python Developed and Supported? Open Source Tradeoffs What Are Python’s Technical Strengths? It’s Object-Oriented and Functional It’s Free It’s Portable It’s Powerful It’s Mixable It’s Relatively Easy to Use It’s Relatively Easy to Learn It’s Named After Monty Python 5 10 11 11 11 12 12 13 13 14 15 15 16 16 17 17 18 19 19 20 20 v How Does Python Stack Up to Language X? Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 21 22 23 23 How Python Runs Programs 27 Introducing the Python Interpreter Program Execution The Programmer’s View Python’s View Execution Model Variations Python Implementation Alternatives Execution Optimization Tools Frozen Binaries Future Possibilities? Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 27 28 28 30 33 33 37 39 40 40 41 41 How You Run Programs 43 The Interactive Prompt Starting an Interactive Session The System Path New Windows Options in 3.3: PATH, Launcher Where to Run: Code Directories What Not to Type: Prompts and Comments Running Code Interactively Why the Interactive Prompt? Usage Notes: The Interactive Prompt System Command Lines and Files A First Script Running Files with Command Lines Command-Line Usage Variations Usage Notes: Command Lines and Files Unix-Style Executable Scripts: #! Unix Script Basics The Unix env Lookup Trick The Python 3.3 Windows Launcher: #! Comes to Windows Clicking File Icons Icon-Click Basics Clicking Icons on Windows The input Trick on Windows Other Icon-Click Limitations vi | Table of Contents 43 44 45 46 47 48 49 50 52 54 55 56 57 58 59 59 60 60 62 62 63 63 66 Module Imports and Reloads Import and Reload Basics The Grander Module Story: Attributes Usage Notes: import and reload Using exec to Run Module Files The IDLE User Interface IDLE Startup Details IDLE Basic Usage IDLE Usability Features Advanced IDLE Tools Usage Notes: IDLE Other IDEs Other Launch Options Embedding Calls Frozen Binary Executables Text Editor Launch Options Still Other Launch Options Future Possibilities? Which Option Should I Use? Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers Test Your Knowledge: Part I Exercises 66 66 68 71 72 73 74 75 76 77 78 79 81 81 82 82 82 83 83 85 85 86 87 Part II Types and Operations Introducing Python Object Types 93 The Python Conceptual Hierarchy Why Use Built-in Types? Python’s Core Data Types Numbers Strings Sequence Operations Immutability Type-Specific Methods Getting Help Other Ways to Code Strings Unicode Strings Pattern Matching Lists Sequence Operations Type-Specific Operations 93 94 95 97 99 99 101 102 104 105 106 108 109 109 109 Table of Contents | vii Bounds Checking Nesting Comprehensions Dictionaries Mapping Operations Nesting Revisited Missing Keys: if Tests Sorting Keys: for Loops Iteration and Optimization Tuples Why Tuples? Files Binary Bytes Files Unicode Text Files Other File-Like Tools Other Core Types How to Break Your Code’s Flexibility User-Defined Classes And Everything Else Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 110 110 111 113 114 115 116 118 120 121 122 122 123 124 126 126 128 129 130 130 131 131 Numeric Types 133 Numeric Type Basics Numeric Literals Built-in Numeric Tools Python Expression Operators Numbers in Action Variables and Basic Expressions Numeric Display Formats Comparisons: Normal and Chained Division: Classic, Floor, and True Integer Precision Complex Numbers Hex, Octal, Binary: Literals and Conversions Bitwise Operations Other Built-in Numeric Tools Other Numeric Types Decimal Type Fraction Type Sets Booleans viii | Table of Contents 133 134 136 136 141 141 143 144 146 150 151 151 153 155 157 157 160 163 171 Download from Wow! eBook pseudoprivate class attributes, 944–947 Python and, 16, 129, 554, 933 quiz questions and answers, 795 realistic example of classes, 817–857 state information, 1232 static and class methods, 1024–1034 super built-in function, 1041–1064 open built-in function customizing, 517–519 file processing and, 122, 126, 283 version considerations, 287 Windows platform and, 286 operations (see specific operations) operator module, 577 operator overloading about, 296, 785, 791, 805, 887, 1238 attributes and, 909–913, 1237–1256 binary operator methods, 917–921 Boolean tests and, 927–929 call expressions and, 921–925 common methods, 888–890 comparisons and, 925–927 constructors and expressions, 888 delegation and, 1322 display formats and, 368 double underscores and, 104 indexing and slicing, 890–894 iteration and, 894–906 membership and, 906–909 metaclasses and, 1374–1378, 1390 object destruction, 929–931 polymorphism and, 141 quiz questions and answers, 931 string representation and, 913–917 super built-in function and, 1047 usage considerations, 808 usage examples, 806–808, 826–828 validating methods, 1327 operator precedence, 139 optimizing objects about, 120, 1159 byte code files, 684 execution optimization tools, 37–38 optparse module, 754 or method, 889 or operator, 384 ord built-in function, 206, 582, 1167 OrderedDict subclass, 256 ordering (see sorting) 1528 | Index ORMs (object relational mappers), 13, 854 os module descriptor files, 295 _exit function, 1153 popen function, 295, 402, 411, 423, 607, 650, 1150 system function, 412, 1150 walk function, 423, 607 OSError class, 1133 OverflowError exception, 1131, 1132 P package imports about, 707–711 all variable, 711 from versus import statement, 713 init .py files, 709–711 relative imports model, 707, 717–733 search path settings, 708, 719 usage considerations, 713–716 usage example, 711–713 version considerations, 718 packages about, 707, 716 all variable, 711 namespace package model, 707, 723, 734– 741 package imports, 708–716 quiz questions and answers, 742 relative imports model, 707, 717–733 search path settings, 708, 719 parameters (see arguments) parentheses ( ) comprehensions and, 112 expression operators and, 139 statements and, 323, 328 superclasses and, 801 tuples and, 277 Parrot project, 40 parsing text in strings, 213 pass statement, 320, 389–390 passing-arguments-by-pointer, 524 passing-arguments-by-value, 524 PATH environment variable about, 1427, 1428 env program and, 60 new Windows options, 46 setting, 45, 57 paths module search paths, 72, 678–684, 723, 756–758, 1431, 1433 package imports, 708 package search paths, 708, 719 recording for recursive calls, 561 pattern matching in strings about, 108 re module and, 108, 215, 1206 pdb command-line debugger, 84, 1159 PEP (Python Enhancement Proposal), 15 percent sign (%) formatting expression operator, 217, 227– 229, 1189 system shell prompt, 44, 48, 56 performance considerations, 589 (see also benchmarking) list comprehensions, 589 MRO and, 1001 program execution, 32 Python alternatives, 35 slots, 1019 Perl programming language, 24 permutations, 612–614 persistence (see object persistence) Peters, Tim, 543 pexpect system, 295 pickle module about, 847 object persistence and, 13, 116 object serialization and, 1209–1211 persistence and, 941 storing objects, 290, 295 plus (+) operator adding numbers, 97 concatenating lists, 242, 1100 concatenating strings, 100, 200, 1100 PMW extension package, 11 polymorphism about, 101, 129, 933 classes and, 792–794, 832 in functions, 479, 482 OOP considerations, 934 operator overloading and, 141 testing exception types, 1151 portability, 17 positional arguments, 529, 534, 1331–1343 pow built-in function, 155 pprint module pformat function, 1006 pprint function, 1006 usage considerations, 1009 precedence rules, 139 print built-in function, 49, 359–361, 547–549 print operations, 49 (see also print statement) about, 358 built-in exception classes and, 1133–1135 completion certificate, 1414–1416 custom displays, 1135 display formats, 826–828 expression statements and, 356 file object methods and, 358 print built-in function, 49, 359–361, 547– 549 print stream redirection, 363–366 quiz questions and answers, 370 standard output stream, 295, 358, 368 version considerations, 359–363, 547–549, 821 version-neutral, 366–368 print statement about, 49, 361–363 common usage mistakes, 52, 59 debugging code and, 84 numeric display formats and, 143 private attributes, 1314–1318 procedures (see functions) profile module, 121, 642, 1158 program architecture about, 67 conceptual hierarchy, 93, 319 modules and, 670–673 program execution about, 27 alternative IDEs, 79–81 alternative launch options, 81–83 byte code compilation and, 30–31 clicking file icons, 62–66 debugging code, 83–85 development considerations, 32 embedding calls, 81 exec built-in function and, 72–73 frozen binaries, 39, 82 future possibilities, 40, 83 IDLE user interface, 73–79 interactive prompt, 31, 43–54 interpreters and, 27–28, 30 model variations in, 33–40 Index | 1529 module imports and reloads, 66–72 optimization tools, 37–38 performance considerations, 32 programmer's perspective, 28–30 PVM and, 31 quiz questions and answers, 41, 85–87 selecting from options, 83 system command lines and files, 54–59 text editor launch options, 82 Unix-style scripts, 59–62 program units, 96 (see also classes; functions; modules) Programming Python (Lutz), 985 programs about, 54 metaprograms, 759–761 modules and, 54, 93 prompts (see interactive prompt; system prompt) properties about, 1020–1023 attribute, 1221–1226 class statement, 798, 861, 1020 coding with decorators, 1224–1226 descriptors and, 1236–1237 validating with, 1256–1259 property built-in function, 1020, 1036, 1220, 1236 prototyping systems, 13 proxy classes (wrappers) about, 942–943 decorators installing, 1270 delegation and, 988 pseudoprivate class attributes about, 845, 944–947 larger projects and, 1160 public attributes and, 1321 PSF (Python Software Foundation), 15 pstats module, 1158 Psyco system, 36, 38 pth file extension, 708 public attributes, 1318–1321 PVM (Python Virtual Machine), 31 py file extension about, 29, 675, 687 common usage mistakes, 78 imported files and, 55 py2app tool, 39 py2exe tool, 39 1530 | Index pyc file extension, 30, 675, 676 pycache subdirectory, 31, 63, 675, 676– 678 PyChecker tool, 661, 1157 PyDev IDE, 80 PyDoc system about, 105, 444 changing colors in, 456 help function, 449–451, 883, 1157 HTML reports, 452–460 version considerations, 452–460 pydoc.py script, 458 pygame toolkit, 39 PyInstaller tool, 39, 1159 PyLint system, 1157 PyMongo interface, 116 pyo file extension, 684 PyPy system about, 7, 34 benchmarking, 652 performance considerations, 35 timeit module and, 643–645 Pyrolog interpreter, 36 PySerial extension, 295 PySolFC program, 15 pystone.py program, 656 Python Enhancement Proposal (PEP), 15 Python interpreter about, 27, 30 additional information, 1436 alternatives to, 36 byte code and, 30–31 configuring, 1427–1436 development considerations, 32 installing, 28, 1421–1425 locating with env program, 60 performance considerations, 32 PVM and, 31 Python programming language additional information, 1436 advantages of, 3–5 common applications of, 10–15 compared to other languages, 21–22 compared to Perl, 24 development community, 15 execution speed, future directions, 853–855 implementation alternatives, 33–36 new Windows options, 46 paradox of, 1409–1414 pillars of programming, 94 portability, 17 quiz questions and answers, 23–24 scripting and, technical strengths, 16–21 tools supporting, 10–15, 19, 1156 tradeoffs using, 8, 15 user base, 9–10, 35 version considerations (see version considerations for Python) Python Software Foundation (PSF), 15 Python Virtual Machine (PVM), 31 PYTHONIOENCODING environment variable, 1205 PythonLauncher, 62 PYTHONPATH environment variable about, 1427, 1428 module search paths, 72, 679, 756 package search paths, 708 PyDoc HTML reports, 458 Windows platform and, 1430 PYTHONSTARTUP environment variable, 1427, 1428 PythonWin IDE, 80 PyUnit tool, 1157 pyw file extension, 46 PY_PYTHON environment variable, 61, 1427, 1429 PY_PYTHON2 environment variable, 1427, 1429 PY_PYTHON3 environment variable, 1427, 1429 Q queue module, 496 queues best-first searches, 560 FIFO, 559 recursion versus, 559, 768 quiz questions and answers chapter 1: Python Q&A session, 23–24 chapter 2: program execution, 41 chapter 3: program execution, 85–87 chapter 4: object types, 131 chapter 5: numbers, 173 chapter 6: dynamic typing, 186 chapter 7: strings, 237 chapter 8: lists and dictionaries, 272 chapter 9: tuples, files, and everything else, 311 chapter 10: statements, 336 chapter 11: assignments, expressions, and prints, 370 chapter 12: if tests and syntax rules, 385 chapter 13: while and for loops, 414 chapter 14: iterations and comprehensions, 441 chapter 15: documentation, 466 chapter 16: functions, 483 chapter 17: scopes, 519–521 chapter 18: arguments, 551 chapter 19: functions, 578 chapter 20: comprehensions and generators, 626 chapter 21: benchmarking, 662 chapter 22: modules, 685 chapter 23: modules, 704 chapter 24: module packages, 742 chapter 25: modules, 777 chapter 26: OOP, 795 chapter 27: classes, 815 chapter 28: classes, 855–857 chapter 29: classes, 884 chapter 30: operator overloading, 931 chapter 31: classes, 978 chapter 32: classes, 1071 chapter 33: exceptions, 1090 chapter 34: exceptions, 1120 chapter 35: exception classes, 1139 chapter 36: exceptions, 1161 chapter 37: Unicode and byte strings, 1215– 1217 chapter 38: attributes, 1266–1268 chapter 39: decorators, 1344–1353 quotation marks interchangeable, 193 multiline block strings, 198–199 strings in, 105, 191 R r file processing mode, 122, 283 radd method, 889, 917–921 raise statement about, 321, 1081, 1106 built-in exceptions and, 1086 chaining exceptions, 1110–1112 from clause, 1110–1112 Index | 1531 propagating exceptions, 1110 raising exceptions, 549, 896, 1086, 1107 raising instances, 1126 signaling conditions with, 1147 version considerations, 1107 random module about, 98, 156 generator example, 616 range built-in function counter loops, 402 iteration and, 434, 435 list comprehensions and, 112, 242, 406 loop coding techniques and, 344, 402–407 nonexhaustive traversals, 405 sequence scans, 403 sequence shufflers, 404 timing calls example, 1298 rapid development cycle, 6, 33 rational numbers, 97, 127 raw_input built-in function, 64, 65, 330 re module about, 96 findall function, 1212 match function, 192, 1212 pattern matching and, 108, 215, 1206 search function, 1212 read-only descriptors, 1228 recursive comparisons, 300 recursive functions about, 487, 555, 880 coding alternatives, 556 from statement and, 775 generators and, 968 handling arbitrary structures, 558–561 loops versus, 557 reloaders, 764–767 summation with, 555 usage examples, 561 reduce built-in function, 430, 576 reference counters, 177, 179 references, 180 (see also shared references) about, 177, 500 assignments and, 339 attribute, 909 circular, 179 copies versus, 297–300, 308 cyclic, 179 dynamic typing and, 176, 180–185 1532 | Index string method calls, 231 weak, 185 relative imports model about, 707, 717–720 absolute imports versus, 722 lookup rules summary, 723 pitfalls of, 729–733 scope of, 722 usage considerations, 720–722 usage examples, 723–728 version considerations, 721 reload built-in function about, 66–71, 700–703 common usage mistakes, 71, 78 from statement and, 773–775 usage examples, 763–770 repetition as programming pillar, 94 in strings, 100, 200, 242 in tuples, 277 usage considerations, 309 repr built-in function about, 205 display formats, 144 string formatting method calls and, 224 version considerations, 138 repr method about, 889, 913–917 custom print displays, 1135 inheritance and, 842, 966 print display example, 826–828 recursive looping and, 561 reserved words, 352 reStructuredText markup language, 461 return statement about, 320, 477 coding functions, 475 function gotchas, 660 returning multiple values, 527 yield statement versus, 592–597 reversed built-in function, 248, 401 RIAs (rich Internet applications), 12 rich Internet applications (RIAs), 12 rmod method, 1189 round built-in function, 149, 156 running programs (see program execution) S SAX parsing, 1212 ScientificPython programming extension, 14 SciPy programming extension, 14, 111 scopes about, 485–488 accessing global variables, 498 builtins module, 491–493 comprehension variables and, 623 function-related gotchas, 661 global statement, 320, 476, 487, 494, 509 imports versus, 698 LEGB rule and, 488, 492, 872, 875–877 in methods and classes, 1068 minimizing cross-file changes, 497–498 minimizing global variables, 495 modules and, 695 name resolution and, 488–490 nested functions and, 499–508, 572 nonlocal statement, 320, 476, 487, 494, 508–517 quiz questions and answers, 519–521 relative imports model, 722 try/except statement and, 1108 usage example, 490 screen scraping technique, 854 scripts and scripting about, common usage mistakes, 78 error handling, 66 executable, 59–62 Internet, 11 launching scripts with icon clicks, 62–66 modules and, 54 Python support, 16 running with arguments, 1432 terminating compound statements, 53 timeit module and, 644, 647–651 timing script, 634 writing scripts, 55 search path modules, 72, 678–684, 756–758, 1431, 1433 packages, 708, 719 selection as programming pillar, 94 self argument about, 554, 863 coding constructors, 818 lambda callbacks and, 573 static methods and, 1025 usage considerations, 811 semicolon (;), 323, 327 sentinel value, 1147 sequence assignments about, 340–342 advanced patterns, 342–344 sequence operations bytes string type, 1190 dictionaries and, 258 generating scrambled sequences, 609–614 iteration and, 434 lists, 109 loop coding techniques, 403–405 numbers, 98 statement execution, 375 strings, 99–101 sequences about, 99 escape, 105, 191, 193–197 intersecting, 480–483, 545–547 list, 109, 243 as programming pillar, 94 repeating, 309 string, 99 tuple, 121 server connections, closing, 1148 set built-in function, 126, 167, 306 set comprehensions, 166, 168, 432, 624–626 set method about, 889 descriptors and, 912, 1293 managing attributes, 1227 set notation, 111 setattr built-in function, 572 setattr method about, 889, 909–913, 1220, 1238 attribute assignments and, 861 emulating privacy, 944 private attributes and, 1318 recursive looping and, 561 setitem method, 889, 890–894, 1194 sets about, 97, 126, 163, 169–171, 547 comparison operations, 302 copy method, 299 creating, 126 dictionary views and, 268 frozen, 167 immutable constraints, 167 literals, 96, 166 Index | 1533 version considerations, 164–169 setslice method, 893 shadowing, 661 shared references about, 180–181 arguments and, 524–526 augmented assignments and, 352 equality and, 183–185 in-place changes and, 181–183 multiple-target assignments and, 349 Shed Skin system, 33, 37 shell tools and commands, 6, 11, 411–413 (see also system command lines and files) shelve module about, 793, 847, 848 dictionary interfaces and, 271 exploring shelves interactively, 849–851 object persistence and, 116, 941 open function, 849 pickle module and, 1209–1211 storing objects on database, 848 updating objects, 851 shelves (see access-by-key databases and filesystems) singleton classes, 1301–1303 slice expressions, 891 slice objects, 891 slicing lists, 243, 244–246 nonexhaustive traversals, 405 operator overloading and, 890–893 strings, 100, 201–204 tuples, 277 slots about, 1008, 1010 descriptors and, 1237 example impacts of, 1017 handling generically, 1014–1016 managing attributes, 912 namespace dictionaries and, 1011–1013 private attributes and, 1318 speed considerations, 1019 superclasses and, 1013 usage rules, 1016 socket module, 96 sorted built-in function about, 118 dictionaries and, 269, 303 iteration and, 430 1534 | Index lists and, 248 tuples and, 278 sorting keys, 118–119, 256, 269 lists, 246–248 version considerations, 247, 302 source code about, 30 timestamps in, 31 spaces versus tabs, 378 Sphinx tool, 444, 461 SQL database API, 590 square brackets [ ], 96, 224, 328 square roots, 156 stack traces, 1084, 1099 Stackless Python, 11, 35 stacks inspecting, 1328–1329 LIFO, 249, 559 limiting depth of, 561 recursion versus, 559, 768 standard error stream (stderr), 366, 930 standard input stream (stdin), 369 standard library about, launch options, 82 standard output stream (stdout), 295, 358, 368 state information about, 129 built-in exception classes and, 1133–1135 class decorators and, 1317 in descriptors, 1232–1235 factory functions and, 501 function attributes and, 565 function decorators and, 1285–1289 generator functions, 592 nonlocal statement and, 512–517 recursive functions and, 560 validating with descriptors and, 1259–1263 statements about, 319 assignment, 320, 339–370 colon character and, 322 common usage mistakes, 53 compound (see compound statements) continuation lines in, 328, 378 control-flow, 375 delimiting, 378 expressions and, 93, 356–357 indenting, 324–327 interactive loops example, 329–336 listed, 320–322 modules and, 54, 93, 771 parentheses and, 323, 328 Python syntax model, 322–329 quiz questions and answers, 336 semicolon and, 323 special case rules, 327–329, 379–380 syntax rules, 375–380 terminating, 323 version considerations, 321 static methods about, 865, 1024 alternatives for, 1027 counting instances, 1030 usage considerations, 1028–1030 version considerations, 1025–1027 staticmethod built-in function, 951, 1024, 1029–1030 steps in slicing, 203 StopIteration exception, 417, 420, 593 storing objects and data binary data, 293 class building example, 847–853 class gotchas, 1069 in files, 288–290 in JSON format, 291–292 pickle module, 290, 295 on shelve database, 848 strings, 1170 struct module, 293 str built-in function about, 205, 306 display formats, 144 string formatting method calls and, 224 usage example, 98 version considerations, 138 str method about, 889, 913–917 custom print displays, 1135 inheritance and, 808, 966 print display example, 826–828, 843 str string type about, 1171–1173 converting, 1192 encoded text, 1183 re module and, 1206 text files and, 287 Unicode literals, 106, 190, 1176 version considerations, 194 stream redirection, 57 strides in slicing, 203 string formatting about, 103, 144, 1190 converting integers to strings, 152 expressions technique, 216–222 literals, 191 method calls technique, 222–234 nesting, 225 type codes, 218–220 string module about, 215–216 relative imports examples, 719–722, 724– 728 strings (str object) about, 96, 99, 189, 1167–1174 add method, 104 alternate ways to code, 105 backslash characters, 193–197 casefold method, 247 changing, 102, 208, 211–213 coding, 1174–1178 common operations, 190–201 comparison operations, 302 concatenating, 100, 104, 200, 1100 converting, 152, 192, 205–207 debugging, 972 decode method, 192, 1177 documentation, 199, 375, 444, 446–449 empty strings, 191 encode method, 192, 1169, 1177 endswith method, 192, 214 exceptions based on, 1124 find method, 102, 191, 200, 212 format method, 222, 225, 227–234, 968 formatting (see string formatting) garbage collection and, 208 immutable, 101, 191, 208 importing modules by name string, 761– 763 indexing, 99, 201–204 isdigit method, 192, 332 join method, 192, 213, 431, 598 literals, 96, 190–199, 378, 1175–1177 lower method, 192, 247 multiline block strings, 198–199 Index | 1535 nonexhaustive traversals, 405 operator overloading and, 913–917 parsing text, 213 pattern matching, 108, 215 quiz questions and answers, 237, 1215– 1217 repeating, 100, 200 replace method, 102, 191, 208, 211 rstrip method, 191, 205, 289, 426 sequence operations, 99–101 slicing, 100, 201–204 split method, 191, 213, 289 tool changes, 1206–1214 type and content mismatches, 1198 type-specific methods, 102, 191, 209–216 Unicode, 106–108, 189, 754–756, 1178– 1188 upper method, 103, 632 version considerations, 190, 194, 215–216, 229, 968, 1165–1167, 1204 strong typing, 97 struct module, 124, 293, 1172, 1207–1209 sub method, 888 subclasses about, 787 class interface techniques, 867–868 coding, 828 customizing behavior, 802, 828–834 extending built-in types, 981–983 inheritance and, 808, 866 type object and, 1366 subprocess module, 295, 413 substitution operations in string formatting, 103 sum built-in function, 112, 155, 555 super built-in function about, 831, 865, 1041 basic usage and tradeoffs, 1043–1049 debates about, 1041–1042 multiple inheritance and, 1043–1046, 1050–1062 operator overloading and, 1047 runtime class changes, 1049 summary of, 1062 version considerations, 1048 superclasses about, 787 abstract, 869–871, 939 class gotchas, 1069 1536 | Index class interface techniques, 867–868 constructor methods, 864 customizing, 833 inheritance and, 866 metaclasses versus, 1381 multiple inheritance and, 956 operator overloading methods and, 1325 parentheses and, 801 slots and, 1013 traditional forms, 1042 sys module argv attribute, 204, 650, 751 excepthook function, 1149 exc_info function, 1138, 1149, 1150–1152 exit function, 1146, 1153 file name settings, 1205 getrecursionlimit function, 561 modules dictionary, 498, 676, 759 path list, 72, 682, 719, 723, 756 platform attribute, 55, 223, 631 setrecursionlimit function, 561 stderr attribute, 366, 930 stdin attribute, 369 stdout attribute, 295, 358, 363–366, 368 system command lines and files about, 54, 1432–1436 common usage mistakes, 58 running files with command lines, 56 running in Python, 294, 411–413, 423, 650, 770 starting interactive sessions, 44 system shell prompt, 44, 48 timeit module and, 644 usage variations, 57 writing scripts, 55 system shell prompt about, 44, 48 running files, 56 SystemExit exception, 1146, 1153 systeminfo command, 412 systems programming, 11 T -t command-line flag, 378 tabs versus spaces, 378 TCL_LIBRARY environment variable, 1427 termination actions about, 1083, 1087–1088 default exception handler and, 1084 Download from Wow! eBook try/finally statement and, 1083, 1087–1088, 1100–1102, 1152 with/as statement and, 1083, 1088, 1152 testing error handling, 332–334 filtering results, 427 from statement and, 774 interactive prompt and, 51 list comprehensions and, 583–586 for missing keys, 117 with name attribute, 750 for positional arguments, 1331–1343 processes, 819–820, 1149 reloading variants, 769 timing calls example, 1297 truth values, 171, 305, 380–382, 927–929 type, 986, 992–995, 1342 text editor launch options, 82 text files about, 107, 1173–1174, 1196 binary files and, 123 creating, 122 Unicode, 124–126, 190, 754–756 version considerations, 287, 1197 _thread module, 496 threading module, 496 3to2 converter, 367 time module about, 121, 543 clock function, 631, 633, 644, 1297 homegrown timing module and, 630 perf_counter function, 633, 1297 process_time function, 633 time function, 631, 633 timeit module about, 121, 543, 642–647 benchmark and script, 647–651 other examples, 890, 1019, 1297, 1434, 1484 repeat function, 643 setup code, 646, 654 timestamps in source code, 31 timing calls with function decorators, 1295– 1301 timing iterations alternatives for, 629–642 timeit module, 642–655 timsort algorithm, 543 tkinter GUI toolkit about, 11 callbacks and, 923, 953 common usage mistakes, 79 configuring Python and, 1428 IDLE and, 74 keyword arguments and, 550 lambda callbacks, 573 quit function, 79 testing reloading variants, 769 TK_LIBRARY environment variable, 1427 traceback module, 1151 traceback objects, 1150, 1151 translation (see encoding and decoding) triple quotes, 198–199 true value in Python Booleans and, 171, 304–305, 380–382 built-in scope and, 494 operator overloading and, 927–929 truncating division, 146–150 try statement about, 1094–1095 catching built-in exceptions, 1100 clauses supported, 1095–1098 debugging with, 1149 default behavior, 1098 wrapping statements with, 1152 try/except statement about, 321, 1081 catching exceptions, 1084, 1088, 1096– 1097, 1100 error handling, 333–334 nesting, 1141–1145 scopes and, 1108 try/except/else statement, 1093–1100, 1147, 1150 try/except/finally statement, 1102–1106, 1143–1145 try/finally statement about, 321, 1081 closing files and server connections, 1148 closing files example, 294 termination actions, 1083, 1087–1088, 1100–1102, 1152 -tt command-line flag, 378 tuple built-in function, 279, 306, 431 tuple-unpacking assignments, 340, 396–398 tuples about, 70, 121–122, 276 assignments and, 591 Index | 1537 common operations, 276, 277–279 comparison operations, 302 concatenating, 277 converting, 278–279 count method, 277, 279 empty, 276 exception hierarchies and, 1129 immutable, 121, 278–279 index method, 277, 279 indexing, 277 iteration in, 277 lists versus, 279 literals, 96, 276 named, 122, 256, 277, 280–282 nesting, 277 quiz questions and answers, 311 repeating, 277 slicing, 277 tuple keys, 259 type-specific methods, 278–279 2to3 converter, 366 type built-in function, 128, 306, 986, 992–995 type designators, 177 type object classes as instances of, 1364–1366 inheritance and, 1379 metaclasses as subclasses of, 1366 TypeError exception, 1100, 1356 types module, 306, 764 U unbound methods, 948–953, 1025 underscore (_) class names, 845 module names, 70, 747 name mangling and, 945 operator overloading, 104, 805 showing name values, 971 Unicode character set character code conversions, 207 code points, 106, 194, 206, 1170 currency symbols, 754–756 encoding and decoding, 123, 190, 192, 1178–1188, 1199–1206 JSON format and, 292 literals and, 106, 190, 1176 quiz questions and answers, 1215–1217 strings and, 106–108, 189, 754–756, 1178– 1188 1538 | Index text files and, 124–126, 190, 754–756 unicode string type about, 106, 190, 287, 1171–1173 coding strings with, 1185–1187 converting, 1192 re module and, 1206 unittest module, 750, 1158 Unix platform awk utility, 413 configuring Python, 1430 env program, 60 executable scripts, 59–62 frozen binaries, 39 GUI support, 11 icon clicks, 62 IDLE startup details, 75 installing Python, 28, 1425 system shell prompt, 44 Windows launcher and, 1437 working directory, 48 Unladen Swallow project, 40 unpacking arguments, 528, 535 user-defined classes, 129 user-defined exceptions, 1086, 1134, 1147 UTF-16 encoding, 1170 UTF-8 encoding, 1168 V validating attributes, 1256–1266 function arguments, 1330–1343 operator overloading methods, 1327 value equality operators, 137 van Rossum, Guido, 17 varargs, 529, 536–537 variables about, 177 assigning values to, 50, 99, 176, 177 attributes and, 68 comprehension, 490, 623 creating, 99, 176 dynamic typing and, 176–178 exception, 490 expressions and, 176 from * statement and, 773 function-related gotchas, 661 global, 495, 498, 745 local, 483 name collisions and, 71 name rules for, 352–355 numbers in, 141–143 objects and, 176 scope of, 486 shared references and, 180–185 try/except statement and, 1108 version considerations for Python about, xxxvi–xxxix abstract superclasses, 870 Booleans, 928 builtins module, 156, 493 classes, 983 comparisons and sorts, 247, 302 context managers, 1118–1119 dictionaries, 264–271, 303 division operations, 146–148 exception classes, 1123 expression operators, 138 files, 287, 1197 function attributes, 515–517 iteration, 419, 434–440, 896 keyword arguments, 539–542 map built-in function, 408 metaclasses, 1369–1370 next method, 593 nonlocal statement, 508–512 package imports, 718 printing, 359–363, 547–549, 821 PyDoc system, 452–460 raise statement, 1107 relative imports model, 721 sets, 164–169 statements, 321 static methods, 1025–1027 storing strings in memory, 1170 strings, 190, 194, 215–216, 229, 968, 1165– 1167, 1204 summarized, 1451–1463 super built-in function, 1048 threading, 496 unbound methods, 950 wrapper classes, 943 view objects, 266–269, 439–440 virtual attributes, 1014–1016 virtual concatenation, 737 W w file processing mode, 122, 283 warnings module, 1147 weak references, 185 weakref module, 185 websites, 462, 853 while statement about, 119, 320, 387 C language, 394 general format, 388 interactive loops example, 330 iteration and, 418 quiz questions and answers, 414 recursion versus, 557 sequence scans, 403 usage examples, 388 whitespace, 191 win32all package, 716 Windows launcher #! comment support, 60–62 about, 46, 57, 1439–1441, 1450 command lines, 1435 icon clicks, 62 pitfalls, 1447–1450 tutorial on, 1441–1447 Unix legacy, 1437 Windows legacy, 1438 Windows platform #! comment support, 60–62 command-line interface, 45, 57 common usage mistakes, 58 configuring Python, 1430 frozen binaries and, 39 GUI support, 11 icon clicks, 62, 63–65 IDLE startup details, 74 installing Python, 28, 1424, 1425–1427 new options, 46 open built-in function, 286 Python documentation, 461 system shell prompt, 44 systeminfo command, 412 win32all package, 716 working directory, 47 Windows Registry Editor, 1431 Wing IDE, 80 Winpdb system, 85 with/as statement about, 321, 1081, 1114–1117 closing files and server connections, 1148 file objects and, 285, 294 resetting precision, 159 Index | 1539 termination actions, 1083, 1088, 1152 version considerations, 1118–1119 working directory, 47 wrappers (proxy classes) about, 942–943 decorators installing, 1270 delegation and, 988 writing scripts, 55 wxPython GUI API, 11 X _x naming convention, 747, 1321 XML parsing tools, 1211–1214 xrange built-in function, 403, 435 Y yield operator, 137 yield statement about, 320 coding example, 902–906 coding functions, 475 extended syntax, 605 function gotchas, 660 generator functions and, 591 iteration and, 423, 440 return statement versus, 592–597 Z ZeroDivisionError exception, 1131, 1132 zip built-in function dictionary keys and, 262, 265 iteration and, 430, 433, 434, 437, 617–621 loop coding techniques and, 402, 407–410 parallel traversals, 407–410 zip file extension, 684 ZODB object-oriented database system, 854 1540 | Index 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 Colophon The animal on the cover of Learning Python, Fifth Edition, is a wood rat (Neotoma Muridae) The wood rat lives in a wide range of conditions (mostly rocky, scrub, and desert areas) over much of North and Central America, generally at some distance from humans Wood rats are good climbers, nesting in trees or bushes up to six meters off the ground; some species burrow underground or in rock crevices or inhabit other species’ abandoned holes These grayish-beige, medium-size rodents are the original pack rats: they carry anything and everything into their homes, whether or not it’s needed, and are especially attracted to shiny objects such as tin cans, glass, and silverware The cover image is a 19th-century engraving from Cuvier’s Animals The cover font is Adobe ITC Garamond The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSansMonoCondensed Download from Wow! eBook ... FIFTH EDITION Download from Wow! eBook Learning Python Mark Lutz Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo Learning Python, Fifth Edition by Mark Lutz Copyright... the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Learning Python, 5th Edition, the image of a wood rat, and related trade dress are trademarks of O’Reilly Media, Inc Many of... Keuren June 2013: Indexer: Lucie Haskins Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest Fifth Edition Revision History for the Fifth Edition: 2013- 06-07

Ngày đăng: 16/11/2019, 21:02

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN