Learning Python: Powerful Object-Oriented Programming ppt

1.2K 5.7K 2
Learning Python: Powerful Object-Oriented Programming ppt

Đ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

www.it-ebooks.info Learning Python www.it-ebooks.info www.it-ebooks.info FOURTH EDITION Learning Python Mark Lutz Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info Learning Python, Fourth Edition by Mark Lutz Copyright © 2009 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: Julie Steele Production Editor: Sumita Mukherji Copyeditor: Rachel Head Production Services: Newgen North America Indexer: John Bickelhaupt Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: March 1999: First Edition. December 2003: Second Edition. October 2007: Third Edition. September 2009: Fourth Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning Python, 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 author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-0-596-15806-4 [M] 1252944666 www.it-ebooks.info To Vera. You are my life. www.it-ebooks.info www.it-ebooks.info Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi Part I. Getting Started 1. A Python Q&A Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Why Do People Use Python? 3 Software Quality 4 Developer Productivity 5 Is Python a “Scripting Language”? 5 OK, but What’s the Downside? 7 Who Uses Python Today? 7 What Can I Do with Python? 9 Systems Programming 9 GUIs 9 Internet Scripting 10 Component Integration 10 Database Programming 11 Rapid Prototyping 11 Numeric and Scientific Programming 11 Gaming, Images, Serial Ports, XML, Robots, and More 12 How Is Python Supported? 12 What Are Python’s Technical Strengths? 13 It’s Object-Oriented 13 It’s Free 13 It’s Portable 14 It’s Powerful 15 It’s Mixable 16 It’s Easy to Use 16 It’s Easy to Learn 17 It’s Named After Monty Python 17 How Does Python Stack Up to Language X? 17 vii www.it-ebooks.info Chapter Summary 18 Test Your Knowledge: Quiz 19 Test Your Knowledge: Answers 19 2. How Python Runs Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Introducing the Python Interpreter 23 Program Execution 24 The Programmer’s View 24 Python’s View 26 Execution Model Variations 29 Python Implementation Alternatives 29 Execution Optimization Tools 30 Frozen Binaries 32 Other Execution Options 33 Future Possibilities? 33 Chapter Summary 34 Test Your Knowledge: Quiz 34 Test Your Knowledge: Answers 34 3. How You Run Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 The Interactive Prompt 35 Running Code Interactively 37 Why the Interactive Prompt? 38 Using the Interactive Prompt 39 System Command Lines and Files 41 A First Script 42 Running Files with Command Lines 43 Using Command Lines and Files 44 Unix Executable Scripts (#!) 46 Clicking File Icons 47 Clicking Icons on Windows 47 The input Trick 49 Other Icon-Click Limitations 50 Module Imports and Reloads 51 The Grander Module Story: Attributes 53 import and reload Usage Notes 56 Using exec to Run Module Files 57 The IDLE User Interface 58 IDLE Basics 58 Using IDLE 60 Advanced IDLE Tools 62 Other IDEs 63 Other Launch Options 64 viii | Table of Contents www.it-ebooks.info Embedding Calls 64 Frozen Binary Executables 65 Text Editor Launch Options 65 Still Other Launch Options 66 Future Possibilities? 66 Which Option Should I Use? 66 Chapter Summary 68 Test Your Knowledge: Quiz 68 Test Your Knowledge: Answers 69 Test Your Knowledge: Part I Exercises 70 Part II. Types and Operations 4. Introducing Python Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Why Use Built-in Types? 76 Python’s Core Data Types 77 Numbers 78 Strings 80 Sequence Operations 80 Immutability 82 Type-Specific Methods 82 Getting Help 84 Other Ways to Code Strings 85 Pattern Matching 85 Lists 86 Sequence Operations 86 Type-Specific Operations 87 Bounds Checking 87 Nesting 88 Comprehensions 88 Dictionaries 90 Mapping Operations 90 Nesting Revisited 91 Sorting Keys: for Loops 93 Iteration and Optimization 94 Missing Keys: if Tests 95 Tuples 96 Why Tuples? 97 Files 97 Other File-Like Tools 99 Other Core Types 99 How to Break Your Code’s Flexibility 100 Table of Contents | ix www.it-ebooks.info [...]... Contents | xxix www.it-ebooks.info www.it-ebooks.info Preface This book provides an introduction to the Python programming language Python is a popular open source programming language used for both standalone programs and scripting applications in a wide variety of domains It is free, portable, powerful, and remarkably easy and fun to use Programmers from every corner of the software industry have found... rather than specific applications of it As such, it’s intended to serve as the first in a two-volume set: • Learning Python, this book, teaches Python itself • Programming Python, among others, shows what you can do with Python after you’ve learned it That is, applications-focused books such as Programming Python pick up where this book leaves off, exploring Python’s role in common domains such as the... edition for 3.0 and 2.6, I’ve also added five new chapters to address new topics and add context: • Chapter 27 is a new class tutorial, using a more realistic example to explore the basics of Python object-oriented programming (OOP) • Chapter 36 provides details on Unicode and byte strings and outlines string and file differences between 3.0 and 2.6 • Chapter 37 collects managed attribute tools such as properties... Function Annotations in 3.0 Anonymous Functions: lambda lambda Basics Why Use lambda? How (Not) to Obfuscate Your Python Code Nested lambdas and Scopes Mapping Functions over Sequences: map Functional Programming Tools: filter and reduce Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 463 465 465 466 467 468 469 469 470 471 472 474 474 475 477 478 479 481 483 483 483 20 Iterations... included here, and it is designed to supplement this book Because of this book’s foundations focus, though, it is able to present Python fundamentals with more depth than many programmers see when first learning the language And because it’s based upon a three-day Python training class with quizzes and exercises throughout, this book serves as a self-paced introduction to the language xxxi www.it-ebooks.info... also freely introduce new features, such as the nonlocal statement in 3.0 and the string format method in 2.6 and 3.0, and will point out when such extensions are not present in older Pythons If you are learning Python for the first time and don’t need to use any legacy code, I encourage you to begin with Python 3.0; it cleans up some longstanding warts in the language, while retaining all the original... from every corner of the software industry have found Python’s focus on developer productivity and software quality to be a strategic advantage in projects both large and small Whether you are new to programming or are a professional developer, this book’s goal is to bring you quickly up to speed on the fundamentals of the core Python language After reading this book, you will know enough about Python . 9 Systems Programming 9 GUIs 9 Internet Scripting 10 Component Integration 10 Database Programming 11 Rapid Prototyping 11 Numeric and Scientific Programming. www.it-ebooks.info Learning Python www.it-ebooks.info www.it-ebooks.info FOURTH EDITION Learning Python Mark Lutz Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info Learning

Ngày đăng: 07/03/2014, 04:20

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Preface

    • About This Fourth Edition

      • Coverage for Both 3.0 and 2.6

      • New Chapters

      • Changes to Existing Material

      • Specific Language Extensions in 2.6 and 3.0

      • Specific Language Removals in 3.0

    • About The Third Edition

      • The Third Edition’s Python Language Changes

      • The Third Edition’s Python Training Changes

      • The Third Edition’s Structural Changes

      • The Third Edition’s Scope Changes

    • About This Book

      • This Book’s Prerequisites

      • This Book’s Scope and Other Books

      • This Book’s Style and Structure

    • Book Updates

    • About the Programs in This Book

    • Using Code Examples

    • Font Conventions

    • Safari® Books Online

    • How to Contact Us

    • Acknowledgments

  • 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

        • Gaming, Images, Serial Ports, XML, Robots, and More

      • How Is Python Supported?

      • What Are Python’s Technical Strengths?

        • It’s Object-Oriented

        • It’s Free

        • It’s Portable

        • It’s Powerful

        • It’s Mixable

        • It’s Easy to Use

        • It’s Easy to Learn

        • 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

          • Jython

          • IronPython

        • Execution Optimization Tools

          • The Psyco just-in-time compiler

          • The Shedskin C++ translator

        • Frozen Binaries

        • Other Execution Options

        • Future Possibilities?

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 3. How You Run Programs

      • The Interactive Prompt

        • Running Code Interactively

        • Why the Interactive Prompt?

          • Experimenting

          • Testing

        • Using the Interactive Prompt

          • Entering multiline statements

      • System Command Lines and Files

        • A First Script

        • Running Files with Command Lines

        • Using Command Lines and Files

        • Unix Executable Scripts (#!)

      • Clicking File Icons

        • Clicking Icons on Windows

        • The input Trick

        • Other Icon-Click Limitations

      • Module Imports and Reloads

        • The Grander Module Story: Attributes

          • Modules and namespaces

        • import and reload Usage Notes

      • Using exec to Run Module Files

      • The IDLE User Interface

        • IDLE Basics

        • Using IDLE

        • Advanced IDLE Tools

      • 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

      • 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

        • Pattern Matching

      • Lists

        • Sequence Operations

        • Type-Specific Operations

        • Bounds Checking

        • Nesting

        • Comprehensions

      • Dictionaries

        • Mapping Operations

        • Nesting Revisited

        • Sorting Keys: for Loops

        • Iteration and Optimization

        • Missing Keys: if Tests

      • Tuples

        • Why Tuples?

      • 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

        • Hexadecimal, Octal, and Binary Notation

        • Bitwise Operations

        • Other Built-in Numeric Tools

      • Other Numeric Types

        • Decimal Type

          • The basics

          • Setting precision globally

          • Decimal context manager

        • Fraction Type

          • The basics

          • Numeric accuracy

          • Conversions and mixed types

        • Sets

          • Set basics in Python 2.6

          • Set literals in Python 3.0

          • Immutable constraints and frozen sets

          • Set comprehensions in Python 3.0

          • 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. Strings

      • String Literals

        • Single- and Double-Quoted Strings Are the Same

        • Escape Sequences Represent Special Bytes

        • 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

      • String Methods

        • String Method Examples: Changing Strings

        • String Method Examples: Parsing Text

        • Other Common String Methods in Action

        • The Original string Module (Gone in 3.0)

      • String Formatting Expressions

        • Advanced String Formatting Expressions

        • Dictionary-Based String Formatting Expressions

      • String Formatting Method Calls

        • The Basics

        • Adding Keys, Attributes, and Offsets

        • Adding Specific Formatting

        • Comparison to the % Formatting Expression

        • Why the New Format Method?

          • Extra features

          • Explicit value references

          • Method names and general arguments

          • Possible future deprecation?

      • 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

          • Other common list operations

      • Dictionaries

      • Dictionaries in Action

        • Basic Dictionary Operations

        • Changing Dictionaries In-Place

        • More Dictionary Methods

        • A Languages Table

        • Dictionary Usage Notes

          • Using dictionaries to simulate flexible lists

          • Using dictionaries for sparse data structures

          • Avoiding missing-key errors

          • Using dictionaries as “records”

        • Other Ways to Make Dictionaries

        • Dictionary Changes in Python 3.0

          • Dictionary comprehensions

          • Dictionary views

          • Dictionary views and sets

          • Sorting dictionary keys

          • Dictionary magnitude comparisons no longer work

          • The has_key method is dead: 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?

      • Files

        • Opening Files

        • Using Files

        • Files in Action

          • Text and binary files in Python 3.0

          • Storing and parsing Python objects in files

          • Storing native Python objects with pickle

          • Storing and parsing packed binary data in files

          • File context managers

        • Other File Tools

      • Type Categories Revisited

      • Object Flexibility

      • References Versus Copies

      • Comparisons, Equality, and Truth

        • Python 3.0 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

      • Python Program Structure 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

        • 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.0

          • 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.0 print Function

          • Call format

          • The 3.0 print function in action

        • The Python 2.6 print Statement

          • Statement forms

          • The 2.6 print statement in action

        • Print Stream Redirection

          • The Python “hello world” program

          • Manual stream redirection

          • Automatic stream redirection

        • Version-Neutral Printing

      • 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

      • Python Syntax Rules

        • Block Delimiters: Indentation Rules

          • Avoid mixing tabs and spaces: New error checking in 3.0

        • Statement Delimiters: Lines and Continuations

        • A Few Special Cases

      • Truth 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.0 extended sequence assignment in for loops

          • Nested for loops

      • Loop Coding Techniques

        • Counter Loops: while and range

        • Nonexhaustive Traversals: range and Slices

        • Changing Lists: range

        • Parallel Traversals: zip and map

          • map equivalence in Python 2.6

          • 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, Part 1

      • Iterators: A First Look

        • The Iteration Protocol: File Iterators

        • Manual Iteration: iter and next

        • Other Built-in Type Iterators

      • List Comprehensions: A First Look

        • List Comprehension Basics

        • Using List Comprehensions on Files

        • Extended List Comprehension Syntax

      • Other Iteration Contexts

      • New Iterables in Python 3.0

        • The range Iterator

        • The map, zip, and filter Iterators

        • Multiple Versus Single Iterators

        • Dictionary View Iterators

      • Other Iterator 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

          • Built-in docstrings

        • PyDoc: The help Function

        • PyDoc: HTML Reports

        • 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

    • 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 Rules

        • Name Resolution: The LEGB Rule

        • Scope Example

        • The Built-in Scope

      • The global Statement

        • Minimize Global Variables

        • Minimize Cross-File Changes

        • Other Ways to Access Globals

      • Scopes and Nested Functions

        • Nested Scope Details

        • Nested Scope Examples

          • Factory functions

          • Retaining enclosing scopes’ state with defaults

          • Nested scopes and lambdas

          • Scopes versus defaults with loop variables

          • Arbitrary scope nesting

      • The nonlocal Statement

        • nonlocal Basics

        • nonlocal in Action

          • Using nonlocal for changes

          • Boundary cases

        • Why nonlocal?

          • Shared state with globals

          • State with classes (preview)

          • State with function attributes

      • 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

      • Special Argument-Matching Modes

        • The Basics

        • Matching Syntax

        • The Gritty Details

        • Keyword and Default Examples

          • Keywords

          • Defaults

          • Combining keywords and defaults

        • Arbitrary Arguments Examples

          • Collecting arguments

          • Unpacking arguments

          • Applying functions generically

          • The defunct apply built-in (Python 2.6)

        • Python 3.0 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.0 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

      • Function Objects: Attributes and Annotations

        • Indirect Function Calls

        • Function Introspection

        • Function Attributes

        • Function Annotations in 3.0

      • Anonymous Functions: lambda

        • lambda Basics

        • Why Use lambda?

        • How (Not) to Obfuscate Your Python Code

        • Nested lambdas and Scopes

      • Mapping Functions over Sequences: map

      • Functional Programming Tools: filter and reduce

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 20. Iterations and Comprehensions, Part 2

      • List Comprehensions Revisited: Functional Tools

        • List Comprehensions Versus map

        • Adding Tests and Nested Loops: filter

        • List Comprehensions and Matrixes

        • Comprehending List Comprehensions

      • Iterators Revisited: Generators

        • Generator Functions: yield Versus return

          • State suspension

          • Iteration protocol integration

          • Generator functions in action

          • Extended generator function protocol: send versus next

        • Generator Expressions: Iterators Meet Comprehensions

        • Generator Functions Versus Generator Expressions

        • Generators Are Single-Iterator Objects

        • Emulating zip and map with Iteration Tools

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

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

        • Value Generation in Built-in Types and Classes

      • 3.0 Comprehension Syntax Summary

        • Comprehending Set and Dictionary Comprehensions

        • Extended Comprehension Syntax for Sets and Dictionaries

      • Timing Iteration Alternatives

        • Timing Module

        • Timing Script

        • Timing Results

        • Timing Module Alternatives

          • Using keyword-only arguments in 3.0

        • Other Suggestions

      • Function Gotchas

        • Local Names Are Detected Statically

        • Defaults and Mutable Objects

        • Functions Without returns

        • Enclosing Scope Loop Variables

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part IV Exercises

  • Part V. Modules

    • Chapter 21. 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

      • The Module Search Path

        • Configuring the Search Path

        • Search Path Variations

        • The sys.path List

        • Module File Selection

        • Advanced Module Selection Concepts

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 22. Module Coding Basics

      • Module Creation

      • Module Usage

        • The import Statement

        • The from Statement

        • The from * Statement

        • Imports Happen Only Once

        • import and from Are Assignments

        • Cross-File Name Changes

        • import and from Equivalence

        • Potential Pitfalls of the from Statement

          • When import is required

      • Module Namespaces

        • Files Generate Namespaces

        • 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 23. Module Packages

      • Package Import Basics

        • Packages and Search Path Settings

        • Package __init__.py Files

      • Package Import Example

        • from Versus import with Packages

      • Why Use Package Imports?

        • A Tale of Three Systems

      • Package Relative Imports

        • Changes in Python 3.0

        • Relative Import Basics

        • Why Relative Imports?

          • The relative imports solution in 3.0

          • 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

          • Imports are still relative to the CWD (again)

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 24. Advanced Module Topics

      • Data Hiding in Modules

        • Minimizing from * Damage: _X and __all__

      • Enabling Future Language Features

      • Mixed Usage Modes: __name__ and __main__

        • Unit Tests with __name__

        • Using Command-Line Arguments with __name__

      • Changing the Module Search Path

      • The as Extension for import and from

      • Modules Are Objects: Metaprograms

      • Importing Modules by Name String

      • Transitive Module Reloads

      • Module Design Concepts

      • Module Gotchas

        • 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 25. OOP: The Big Picture

      • Why Use Classes?

      • OOP from 30,000 Feet

        • Attribute Inheritance Search

        • Classes and Instances

        • Class Method Calls

        • Coding Class Trees

        • OOP Is About Code Reuse

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 26. 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

        • Why Use Operator Overloading?

      • The World’s Simplest Python Class

        • Classes Versus Dictionaries

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 27. 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

        • 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 28. 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

        • Python 2.6 and 3.0 Abstract Superclasses

      • Namespaces: The Whole Story

        • Simple Names: Global Unless Assigned

        • Attribute Names: Object Namespaces

        • The “Zen” of Python Namespaces: Assignments Classify Names

        • Namespace Dictionaries

        • Namespace Links

      • Documentation Strings Revisited

      • Classes Versus Modules

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 29. Operator Overloading

      • The Basics

        • Constructors and Expressions: __init__ and __sub__

        • Common Operator Overloading Methods

      • Indexing and Slicing: __getitem__ and __setitem__

        • Intercepting Slices

      • Index Iteration: __getitem__

      • Iterator Objects: __iter__ and __next__

        • User-Defined Iterators

        • Multiple Iterators on One Object

      • Membership: __contains__, __iter__, and __getitem__

      • Attribute Reference: __getattr__ and __setattr__

        • Other Attribute Management Tools

        • Emulating Privacy for Instance Attributes: Part 1

      • String Representation: __repr__ and __str__

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

        • In-Place Addition

      • Call Expressions: __call__

        • Function Interfaces and Callback-Based Code

      • Comparisons: __lt__, __gt__, and Others

        • The 2.6 __cmp__ Method (Removed in 3.0)

      • Boolean Tests: __bool__ and __len__

      • Object Destruction: __del__

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 30. Designing with Classes

      • Python and OOP

        • Overloading by Call Signatures (or Not)

      • OOP and Inheritance: “Is-a” Relationships

      • OOP and Composition: “Has-a” Relationships

        • Stream Processors Revisited

      • OOP and Delegation: “Wrapper” Objects

      • Pseudoprivate Class Attributes

        • Name Mangling Overview

        • Why Use Pseudoprivate Attributes?

      • Methods Are Objects: Bound or Unbound

        • Unbound Methods are Functions in 3.0

        • Bound Methods and Other Callable Objects

      • 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

      • Classes Are Objects: Generic Object Factories

        • Why Factories?

      • Other Design-Related Topics

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 31. Advanced Class Topics

      • Extending Built-in Types

        • Extending Types by Embedding

        • Extending Types by Subclassing

      • The “New-Style” Class Model

      • New-Style Class Changes

        • Type Model Changes

          • Implications for type testing

          • All objects derive from “object”

        • Diamond Inheritance Change

          • Diamond inheritance example

          • Explicit conflict resolution

          • Scope of search order change

      • New-Style Class Extensions

        • Instance Slots

          • Slots and generic code

          • Multiple __slot__ lists in superclasses

        • Class Properties

        • __getattribute__ and Descriptors

        • Metaclasses

      • Static and Class Methods

        • Why the Special Methods?

        • Static Methods in 2.6 and 3.0

        • 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 Function Decorator Example

        • Class Decorators and Metaclasses

        • For More Details

      • Class Gotchas

        • Changing Class Attributes Can Have Side Effects

        • Changing Mutable Class Attributes Can Have Side Effects, Too

        • Multiple Inheritance: Order Matters

        • Methods, Classes, and Nested Scopes

        • Delegation-Based Classes in 3.0: __getattr__ and built-ins

        • “Overwrapping-itis”

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

      • Test Your Knowledge: Part VI Exercises

  • Part VII. Exceptions and Tools

    • Chapter 32. 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 33. Exception Coding Details

      • The try/except/else Statement

        • try Statement Clauses

        • 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

        • Propagating Exceptions with raise

        • Python 3.0 Exception Chaining: raise from

      • The assert Statement

        • Example: Trapping Constraints (but Not Errors!)

      • with/as Context Managers

        • Basic Usage

        • The Context Management Protocol

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 34. 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 35. Designing with Exceptions

      • Nesting Exception Handlers

        • Example: Control-Flow Nesting

        • Example: Syntactic Nesting

      • Exception Idioms

        • 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

      • 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 36. Unicode and Byte Strings

      • String Changes in 3.0

      • String Basics

        • Character Encoding Schemes

        • Python’s String Types

        • Text and Binary Files

      • Python 3.0 Strings in Action

        • Literals and Basic Properties

        • Conversions

      • Coding Unicode Strings

        • Coding ASCII Text

        • Coding Non-ASCII Text

        • Encoding and Decoding Non-ASCII text

        • Other Unicode Coding Techniques

        • Converting Encodings

        • Coding Unicode Strings in Python 2.6

        • Source File Character Set Encoding Declarations

      • Using 3.0 Bytes Objects

        • Method Calls

        • Sequence Operations

        • Other Ways to Make bytes Objects

        • Mixing String Types

      • Using 3.0 (and 2.6) bytearray Objects

      • Using Text and Binary Files

        • Text File Basics

        • Text and Binary Modes in 3.0

        • Type and Content Mismatches

      • Using Unicode Files

        • Reading and Writing Unicode in 3.0

          • Manual encoding

          • File output encoding

          • File input decoding

          • Decoding mismatches

        • Handling the BOM in 3.0

        • Unicode Files in 2.6

      • Other String Tool Changes in 3.0

        • 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 37. Managed Attributes

      • Why Manage Attributes?

        • Inserting Code to Run on Attribute Access

      • Properties

        • The Basics

        • A First Example

        • Computed Attributes

        • Coding Properties with 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

      • __getattr__ and __getattribute__

        • The Basics

          • Avoiding loops in attribute interception methods

        • A First Example

        • Computed Attributes

        • __getattr__ and __getattribute__ Compared

        • Management Techniques Compared

        • Intercepting Built-in Operation Attributes

        • Delegation-Based Managers Revisited

      • Example: Attribute Validations

        • Using Properties to Validate

          • Self-test code

        • Using Descriptors to Validate

        • Using __getattr__ to Validate

        • Using __getattribute__ to Validate

      • Chapter Summary

      • Test Your Knowledge: Quiz

        • Test Your Knowledge: Answers

    • Chapter 38. 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

        • State Information Retention Options

          • Class instance attributes

          • Enclosing scopes and globals

          • Enclosing scopes and nonlocals

          • Function attributes

        • Class Blunders I: Decorating Class Methods

          • Using nested functions to decorate methods

          • Using descriptors to decorate methods

        • Timing Calls

        • Adding Decorator Arguments

      • Coding Class Decorators

        • Singleton Classes

        • Tracing Object Interfaces

        • 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

          • State retention and enclosing scopes

          • Using __dict__ and __slots__

        • Generalizing for Public Declarations, Too

        • Implementation Details II

          • Using __X pseudoprivate names

          • Breaking privacy

          • Decorator tradeoffs

        • Open Issues

          • Caveat: operator overloading methods fail to delegate under 3.0

          • 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

        • Decorator Arguments Versus Function Annotations

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

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

    • Chapter 39. Metaclasses

      • To Metaclass or Not to Metaclass

        • Increasing Levels of Magic

        • 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

      • Coding Metaclasses

        • A Basic Metaclass

        • Customizing Construction and Initialization

        • Other Metaclass Coding Techniques

          • Using simple factory functions

          • Overloading class creation calls with metaclasses

          • Overloading class creation calls with normal classes

        • Instances Versus Inheritance

      • Example: Adding Methods to Classes

        • Manual Augmentation

        • Metaclass-Based Augmentation

        • Metaclasses Versus Class Decorators: Round 2

          • Decorator-based augmentation

          • Managing instances instead of classes

      • 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

      • Chapter Summary

      • Test Your Knowledge: Quiz

      • Test Your Knowledge: Answers

  • 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 (Windows)

          • Windows environment variable GUI

          • Windows registry

          • Path files

        • Python Command-Line Options

      • For More Help

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

      • Part I, Getting Started

      • Part II, Types and Operations

      • Part III, Statements and Syntax

      • Part IV, Functions

      • Part V, Modules

      • Part VI, Classes and OOP

      • Part VII, Exceptions and Tools

  • Index

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan