Learning Python, 5th Edition

Book description

Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.

Complete with quizzes, exercises, and helpful illustrations, this easy-to-follow, self-paced tutorial gets you started with both Python 2.7 and 3.3— the latest releases in the 3.X and 2.X lines—plus all other releases in common use today. You’ll also learn some advanced language features that recently have become more common in Python code.

  • Explore Python’s major built-in object types such as numbers, lists, and dictionaries
  • Create and process objects with Python statements, and learn Python’s general syntax model
  • Use functions to avoid code redundancy and package code for reuse
  • Organize statements, functions, and other tools into larger components with modules
  • Dive into classes: Python’s object-oriented programming tool for structuring code
  • Write large programs with Python’s exception-handling model and development tools
  • Learn advanced Python tools, including decorators, descriptors, metaclasses, and Unicode processing

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. This Book’s “Ecosystem”
    2. About This Fifth Edition
    3. The Python 2.X and 3.X Lines
      1. The 2.X/3.X Story Today
      2. Coverage for Both 3.X and 2.X
      3. Which Python Should I Use?
    4. This Book’s Prerequisites and Effort
    5. This Book’s Structure
    6. What This Book Is Not
      1. It’s Not a Reference or a Guide to Specific Applications
      2. It’s Not the Short Story for People in a Hurry
      3. It’s as Linear as Python Allows
    7. This Book’s Programs
      1. Python Versions
      2. Platforms
      3. Fetching This Book’s Code
      4. Using This Book’s Code
    8. Font Conventions
    9. Book Updates and Resources
    10. Acknowledgments
      1. The Backstory
      2. Python Thanks
      3. Personal Thanks
  2. I. Getting Started
  3. 1. A Python Q&A Session
    1. Why Do People Use Python?
      1. Software Quality
      2. Developer Productivity
    2. Is Python a “Scripting Language”?
    3. OK, but What’s the Downside?
    4. Who Uses Python Today?
    5. What Can I Do with Python?
      1. Systems Programming
      2. GUIs
      3. Internet Scripting
      4. Component Integration
      5. Database Programming
      6. Rapid Prototyping
      7. Numeric and Scientific Programming
      8. And More: Gaming, Images, Data Mining, Robots, Excel...
    6. How Is Python Developed and Supported?
      1. Open Source Tradeoffs
    7. What Are Python’s Technical Strengths?
      1. It’s Object-Oriented and Functional
      2. It’s Free
      3. It’s Portable
      4. It’s Powerful
      5. It’s Mixable
      6. It’s Relatively Easy to Use
      7. It’s Relatively Easy to Learn
      8. It’s Named After Monty Python
    8. How Does Python Stack Up to Language X?
    9. Chapter Summary
    10. Test Your Knowledge: Quiz
    11. Test Your Knowledge: Answers
  4. 2. How Python Runs Programs
    1. Introducing the Python Interpreter
    2. Program Execution
      1. The Programmer’s View
      2. Python’s View
    3. Execution Model Variations
      1. Python Implementation Alternatives
      2. Execution Optimization Tools
      3. Frozen Binaries
      4. Future Possibilities?
    4. Chapter Summary
    5. Test Your Knowledge: Quiz
    6. Test Your Knowledge: Answers
  5. 3. How You Run Programs
    1. The Interactive Prompt
      1. Starting an Interactive Session
      2. The System Path
      3. New Windows Options in 3.3: PATH, Launcher
      4. Where to Run: Code Directories
      5. What Not to Type: Prompts and Comments
      6. Running Code Interactively
      7. Why the Interactive Prompt?
      8. Usage Notes: The Interactive Prompt
    2. System Command Lines and Files
      1. A First Script
      2. Running Files with Command Lines
      3. Command-Line Usage Variations
      4. Usage Notes: Command Lines and Files
    3. Unix-Style Executable Scripts: #!
      1. Unix Script Basics
      2. The Unix env Lookup Trick
      3. The Python 3.3 Windows Launcher: #! Comes to Windows
    4. Clicking File Icons
      1. Icon-Click Basics
      2. Clicking Icons on Windows
      3. The input Trick on Windows
      4. Other Icon-Click Limitations
    5. Module Imports and Reloads
      1. Import and Reload Basics
      2. The Grander Module Story: Attributes
      3. Usage Notes: import and reload
    6. Using exec to Run Module Files
    7. The IDLE User Interface
      1. IDLE Startup Details
      2. IDLE Basic Usage
      3. IDLE Usability Features
      4. Advanced IDLE Tools
      5. Usage Notes: IDLE
    8. Other IDEs
    9. Other Launch Options
      1. Embedding Calls
      2. Frozen Binary Executables
      3. Text Editor Launch Options
      4. Still Other Launch Options
      5. Future Possibilities?
    10. Which Option Should I Use?
    11. Chapter Summary
    12. Test Your Knowledge: Quiz
    13. Test Your Knowledge: Answers
    14. Test Your Knowledge: Part I Exercises
  6. II. Types and Operations
  7. 4. Introducing Python Object Types
    1. The Python Conceptual Hierarchy
    2. Why Use Built-in Types?
    3. Python’s Core Data Types
    4. Numbers
    5. Strings
      1. Sequence Operations
      2. Immutability
      3. Type-Specific Methods
      4. Getting Help
      5. Other Ways to Code Strings
      6. Unicode Strings
      7. Pattern Matching
    6. Lists
      1. Sequence Operations
      2. Type-Specific Operations
      3. Bounds Checking
      4. Nesting
      5. Comprehensions
    7. Dictionaries
      1. Mapping Operations
      2. Nesting Revisited
      3. Missing Keys: if Tests
      4. Sorting Keys: for Loops
      5. Iteration and Optimization
    8. Tuples
      1. Why Tuples?
    9. Files
      1. Binary Bytes Files
      2. Unicode Text Files
      3. Other File-Like Tools
    10. Other Core Types
      1. How to Break Your Code’s Flexibility
      2. User-Defined Classes
      3. And Everything Else
    11. Chapter Summary
    12. Test Your Knowledge: Quiz
    13. Test Your Knowledge: Answers
  8. 5. Numeric Types
    1. Numeric Type Basics
      1. Numeric Literals
      2. Built-in Numeric Tools
      3. Python Expression Operators
    2. Numbers in Action
      1. Variables and Basic Expressions
      2. Numeric Display Formats
      3. Comparisons: Normal and Chained
      4. Division: Classic, Floor, and True
      5. Integer Precision
      6. Complex Numbers
      7. Hex, Octal, Binary: Literals and Conversions
      8. Bitwise Operations
      9. Other Built-in Numeric Tools
    3. Other Numeric Types
      1. Decimal Type
      2. Fraction Type
      3. Sets
      4. Booleans
    4. Numeric Extensions
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  9. 6. The Dynamic Typing Interlude
    1. The Case of the Missing Declaration Statements
      1. Variables, Objects, and References
      2. Types Live with Objects, Not Variables
      3. Objects Are Garbage-Collected
    2. Shared References
      1. Shared References and In-Place Changes
      2. Shared References and Equality
    3. Dynamic Typing Is Everywhere
    4. Chapter Summary
    5. Test Your Knowledge: Quiz
    6. Test Your Knowledge: Answers
  10. 7. String Fundamentals
    1. This Chapter’s Scope
      1. Unicode: The Short Story
    2. String Basics
    3. String Literals
      1. Single- and Double-Quoted Strings Are the Same
      2. Escape Sequences Represent Special Characters
      3. Raw Strings Suppress Escapes
      4. Triple Quotes Code Multiline Block Strings
    4. Strings in Action
      1. Basic Operations
      2. Indexing and Slicing
      3. String Conversion Tools
      4. Changing Strings I
    5. String Methods
      1. Method Call Syntax
      2. Methods of Strings
      3. String Method Examples: Changing Strings II
      4. String Method Examples: Parsing Text
      5. Other Common String Methods in Action
      6. The Original string Module’s Functions (Gone in 3.X)
    6. String Formatting Expressions
      1. Formatting Expression Basics
      2. Advanced Formatting Expression Syntax
      3. Advanced Formatting Expression Examples
      4. Dictionary-Based Formatting Expressions
    7. String Formatting Method Calls
      1. Formatting Method Basics
      2. Adding Keys, Attributes, and Offsets
      3. Advanced Formatting Method Syntax
      4. Advanced Formatting Method Examples
      5. Comparison to the % Formatting Expression
      6. Why the Format Method?
    8. General Type Categories
      1. Types Share Operation Sets by Categories
      2. Mutable Types Can Be Changed in Place
    9. Chapter Summary
    10. Test Your Knowledge: Quiz
    11. Test Your Knowledge: Answers
  11. 8. Lists and Dictionaries
    1. Lists
    2. Lists in Action
      1. Basic List Operations
      2. List Iteration and Comprehensions
      3. Indexing, Slicing, and Matrixes
      4. Changing Lists in Place
    3. Dictionaries
    4. Dictionaries in Action
      1. Basic Dictionary Operations
      2. Changing Dictionaries in Place
      3. More Dictionary Methods
      4. Example: Movie Database
      5. Dictionary Usage Notes
      6. Other Ways to Make Dictionaries
      7. Dictionary Changes in Python 3.X and 2.7
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  12. 9. Tuples, Files, and Everything Else
    1. Tuples
      1. Tuples in Action
      2. Why Lists and Tuples?
      3. Records Revisited: Named Tuples
    2. Files
      1. Opening Files
      2. Using Files
      3. Files in Action
      4. Text and Binary Files: The Short Story
      5. Storing Python Objects in Files: Conversions
      6. Storing Native Python Objects: pickle
      7. Storing Python Objects in JSON Format
      8. Storing Packed Binary Data: struct
      9. File Context Managers
      10. Other File Tools
    3. Core Types Review and Summary
      1. Object Flexibility
      2. References Versus Copies
      3. Comparisons, Equality, and Truth
      4. The Meaning of True and False in Python
      5. Python’s Type Hierarchies
      6. Type Objects
      7. Other Types in Python
    4. Built-in Type Gotchas
      1. Assignment Creates References, Not Copies
      2. Repetition Adds One Level Deep
      3. Beware of Cyclic Data Structures
      4. Immutable Types Can’t Be Changed in Place
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
    8. Test Your Knowledge: Part II Exercises
  13. III. Statements and Syntax
  14. 10. Introducing Python Statements
    1. The Python Conceptual Hierarchy Revisited
    2. Python’s Statements
    3. A Tale of Two ifs
      1. What Python Adds
      2. What Python Removes
      3. Why Indentation Syntax?
      4. A Few Special Cases
    4. A Quick Example: Interactive Loops
      1. A Simple Interactive Loop
      2. Doing Math on User Inputs
      3. Handling Errors by Testing Inputs
      4. Handling Errors with try Statements
      5. Nesting Code Three Levels Deep
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  15. 11. Assignments, Expressions, and Prints
    1. Assignment Statements
      1. Assignment Statement Forms
      2. Sequence Assignments
      3. Extended Sequence Unpacking in Python 3.X
      4. Multiple-Target Assignments
      5. Augmented Assignments
      6. Variable Name Rules
    2. Expression Statements
      1. Expression Statements and In-Place Changes
    3. Print Operations
      1. The Python 3.X print Function
      2. The Python 2.X print Statement
      3. Print Stream Redirection
      4. Version-Neutral Printing
    4. Chapter Summary
    5. Test Your Knowledge: Quiz
    6. Test Your Knowledge: Answers
  16. 12. if Tests and Syntax Rules
    1. if Statements
      1. General Format
      2. Basic Examples
      3. Multiway Branching
    2. Python Syntax Revisited
      1. Block Delimiters: Indentation Rules
      2. Statement Delimiters: Lines and Continuations
      3. A Few Special Cases
    3. Truth Values and Boolean Tests
    4. The if/else Ternary Expression
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  17. 13. while and for Loops
    1. while Loops
      1. General Format
      2. Examples
    2. break, continue, pass, and the Loop else
      1. General Loop Format
      2. pass
      3. continue
      4. break
      5. Loop else
    3. for Loops
      1. General Format
      2. Examples
    4. Loop Coding Techniques
      1. Counter Loops: range
      2. Sequence Scans: while and range Versus for
      3. Sequence Shufflers: range and len
      4. Nonexhaustive Traversals: range Versus Slices
      5. Changing Lists: range Versus Comprehensions
      6. Parallel Traversals: zip and map
      7. Generating Both Offsets and Items: enumerate
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  18. 14. Iterations and Comprehensions
    1. Iterations: A First Look
      1. The Iteration Protocol: File Iterators
      2. Manual Iteration: iter and next
      3. Other Built-in Type Iterables
    2. List Comprehensions: A First Detailed Look
      1. List Comprehension Basics
      2. Using List Comprehensions on Files
      3. Extended List Comprehension Syntax
    3. Other Iteration Contexts
    4. New Iterables in Python 3.X
      1. Impacts on 2.X Code: Pros and Cons
      2. The range Iterable
      3. The map, zip, and filter Iterables
      4. Multiple Versus Single Pass Iterators
      5. Dictionary View Iterables
    5. Other Iteration Topics
    6. Chapter Summary
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  19. 15. The Documentation Interlude
    1. Python Documentation Sources
      1. # Comments
      2. The dir Function
      3. Docstrings: __doc__
      4. PyDoc: The help Function
      5. PyDoc: HTML Reports
      6. Beyond docstrings: Sphinx
      7. The Standard Manual Set
      8. Web Resources
      9. Published Books
    2. Common Coding Gotchas
    3. Chapter Summary
    4. Test Your Knowledge: Quiz
    5. Test Your Knowledge: Answers
    6. Test Your Knowledge: Part III Exercises
  20. IV. Functions and Generators
  21. 16. Function Basics
    1. Why Use Functions?
    2. Coding Functions
      1. def Statements
      2. def Executes at Runtime
    3. A First Example: Definitions and Calls
      1. Definition
      2. Calls
      3. Polymorphism in Python
    4. A Second Example: Intersecting Sequences
      1. Definition
      2. Calls
      3. Polymorphism Revisited
      4. Local Variables
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  22. 17. Scopes
    1. Python Scope Basics
      1. Scope Details
      2. Name Resolution: The LEGB Rule
      3. Scope Example
      4. The Built-in Scope
    2. The global Statement
      1. Program Design: Minimize Global Variables
      2. Program Design: Minimize Cross-File Changes
      3. Other Ways to Access Globals
    3. Scopes and Nested Functions
      1. Nested Scope Details
      2. Nested Scope Examples
      3. Factory Functions: Closures
      4. Retaining Enclosing Scope State with Defaults
    4. The nonlocal Statement in 3.X
      1. nonlocal Basics
      2. nonlocal in Action
    5. Why nonlocal? State Retention Options
      1. State with nonlocal: 3.X only
      2. State with Globals: A Single Copy Only
      3. State with Classes: Explicit Attributes (Preview)
      4. State with Function Attributes: 3.X and 2.X
    6. Chapter Summary
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  23. 18. Arguments
    1. Argument-Passing Basics
      1. Arguments and Shared References
      2. Avoiding Mutable Argument Changes
      3. Simulating Output Parameters and Multiple Results
    2. Special Argument-Matching Modes
      1. Argument Matching Basics
      2. Argument Matching Syntax
      3. The Gritty Details
      4. Keyword and Default Examples
      5. Arbitrary Arguments Examples
      6. Python 3.X Keyword-Only Arguments
    3. The min Wakeup Call!
      1. Full Credit
      2. Bonus Points
      3. The Punch Line...
    4. Generalized Set Functions
    5. Emulating the Python 3.X print Function
      1. Using Keyword-Only Arguments
    6. Chapter Summary
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  24. 19. Advanced Function Topics
    1. Function Design Concepts
    2. Recursive Functions
      1. Summation with Recursion
      2. Coding Alternatives
      3. Loop Statements Versus Recursion
      4. Handling Arbitrary Structures
    3. Function Objects: Attributes and Annotations
      1. Indirect Function Calls: “First Class” Objects
      2. Function Introspection
      3. Function Attributes
      4. Function Annotations in 3.X
    4. Anonymous Functions: lambda
      1. lambda Basics
      2. Why Use lambda?
      3. How (Not) to Obfuscate Your Python Code
      4. Scopes: lambdas Can Be Nested Too
    5. Functional Programming Tools
      1. Mapping Functions over Iterables: map
      2. Selecting Items in Iterables: filter
      3. Combining Items in Iterables: reduce
    6. Chapter Summary
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  25. 20. Comprehensions and Generations
    1. List Comprehensions and Functional Tools
      1. List Comprehensions Versus map
      2. Adding Tests and Nested Loops: filter
      3. Example: List Comprehensions and Matrixes
      4. Don’t Abuse List Comprehensions: KISS
    2. Generator Functions and Expressions
      1. Generator Functions: yield Versus return
      2. Generator Expressions: Iterables Meet Comprehensions
      3. Generator Functions Versus Generator Expressions
      4. Generators Are Single-Iteration Objects
      5. Generation in Built-in Types, Tools, and Classes
      6. Example: Generating Scrambled Sequences
      7. Don’t Abuse Generators: EIBTI
      8. Example: Emulating zip and map with Iteration Tools
    3. Comprehension Syntax Summary
      1. Scopes and Comprehension Variables
      2. Comprehending Set and Dictionary Comprehensions
      3. Extended Comprehension Syntax for Sets and Dictionaries
    4. Chapter Summary
    5. Test Your Knowledge: Quiz
    6. Test Your Knowledge: Answers
  26. 21. The Benchmarking Interlude
    1. Timing Iteration Alternatives
      1. Timing Module: Homegrown
      2. Timing Script
      3. Timing Results
      4. Timing Module Alternatives
      5. Other Suggestions
    2. Timing Iterations and Pythons with timeit
      1. Basic timeit Usage
      2. Benchmark Module and Script: timeit
      3. Benchmark Script Results
      4. More Fun with Benchmarks
    3. Other Benchmarking Topics: pystones
    4. Function Gotchas
      1. Local Names Are Detected Statically
      2. Defaults and Mutable Objects
      3. Functions Without returns
      4. Miscellaneous Function Gotchas
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
    8. Test Your Knowledge: Part IV Exercises
  27. V. Modules and Packages
  28. 22. Modules: The Big Picture
    1. Why Use Modules?
    2. Python Program Architecture
      1. How to Structure a Program
      2. Imports and Attributes
      3. Standard Library Modules
    3. How Imports Work
      1. 1. Find It
      2. 2. Compile It (Maybe)
      3. 3. Run It
    4. Byte Code Files: __pycache__ in Python 3.2+
      1. Byte Code File Models in Action
    5. The Module Search Path
      1. Configuring the Search Path
      2. Search Path Variations
      3. The sys.path List
      4. Module File Selection
    6. Chapter Summary
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  29. 23. Module Coding Basics
    1. Module Creation
      1. Module Filenames
      2. Other Kinds of Modules
    2. Module Usage
      1. The import Statement
      2. The from Statement
      3. The from * Statement
      4. Imports Happen Only Once
      5. import and from Are Assignments
      6. import and from Equivalence
      7. Potential Pitfalls of the from Statement
    3. Module Namespaces
      1. Files Generate Namespaces
      2. Namespace Dictionaries: __dict__
      3. Attribute Name Qualification
      4. Imports Versus Scopes
      5. Namespace Nesting
    4. Reloading Modules
      1. reload Basics
      2. reload Example
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  30. 24. Module Packages
    1. Package Import Basics
      1. Packages and Search Path Settings
      2. Package __init__.py Files
    2. Package Import Example
      1. from Versus import with Packages
    3. Why Use Package Imports?
      1. A Tale of Three Systems
    4. Package Relative Imports
      1. Changes in Python 3.X
      2. Relative Import Basics
      3. Why Relative Imports?
      4. The Scope of Relative Imports
      5. Module Lookup Rules Summary
      6. Relative Imports in Action
      7. Pitfalls of Package-Relative Imports: Mixed Use
    5. Python 3.3 Namespace Packages
      1. Namespace Package Semantics
      2. Impacts on Regular Packages: Optional __init__.py
      3. Namespace Packages in Action
      4. Namespace Package Nesting
      5. Files Still Have Precedence over Directories
    6. Chapter Summary
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  31. 25. Advanced Module Topics
    1. Module Design Concepts
    2. Data Hiding in Modules
      1. Minimizing from * Damage: _X and __all__
    3. Enabling Future Language Features: __future__
    4. Mixed Usage Modes: __name__ and __main__
      1. Unit Tests with __name__
    5. Example: Dual Mode Code
      1. Currency Symbols: Unicode in Action
      2. Docstrings: Module Documentation at Work
    6. Changing the Module Search Path
    7. The as Extension for import and from
    8. Example: Modules Are Objects
    9. Importing Modules by Name String
      1. Running Code Strings
      2. Direct Calls: Two Options
    10. Example: Transitive Module Reloads
      1. A Recursive Reloader
      2. Alternative Codings
    11. Module Gotchas
      1. Module Name Clashes: Package and Package-Relative Imports
      2. Statement Order Matters in Top-Level Code
      3. from Copies Names but Doesn’t Link
      4. from * Can Obscure the Meaning of Variables
      5. reload May Not Impact from Imports
      6. reload, from, and Interactive Testing
      7. Recursive from Imports May Not Work
    12. Chapter Summary
    13. Test Your Knowledge: Quiz
    14. Test Your Knowledge: Answers
    15. Test Your Knowledge: Part V Exercises
  32. VI. Classes and OOP
  33. 26. OOP: The Big Picture
    1. Why Use Classes?
    2. OOP from 30,000 Feet
      1. Attribute Inheritance Search
      2. Classes and Instances
      3. Method Calls
      4. Coding Class Trees
      5. Operator Overloading
      6. OOP Is About Code Reuse
    3. Chapter Summary
    4. Test Your Knowledge: Quiz
    5. Test Your Knowledge: Answers
  34. 27. Class Coding Basics
    1. Classes Generate Multiple Instance Objects
      1. Class Objects Provide Default Behavior
      2. Instance Objects Are Concrete Items
      3. A First Example
    2. Classes Are Customized by Inheritance
      1. A Second Example
      2. Classes Are Attributes in Modules
    3. Classes Can Intercept Python Operators
      1. A Third Example
      2. Why Use Operator Overloading?
    4. The World’s Simplest Python Class
      1. Records Revisited: Classes Versus Dictionaries
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
  35. 28. A More Realistic Example
    1. Step 1: Making Instances
      1. Coding Constructors
      2. Testing As You Go
      3. Using Code Two Ways
    2. Step 2: Adding Behavior Methods
      1. Coding Methods
    3. Step 3: Operator Overloading
      1. Providing Print Displays
    4. Step 4: Customizing Behavior by Subclassing
      1. Coding Subclasses
      2. Augmenting Methods: The Bad Way
      3. Augmenting Methods: The Good Way
      4. Polymorphism in Action
      5. Inherit, Customize, and Extend
      6. OOP: The Big Idea
    5. Step 5: Customizing Constructors, Too
      1. OOP Is Simpler Than You May Think
      2. Other Ways to Combine Classes
    6. Step 6: Using Introspection Tools
      1. Special Class Attributes
      2. A Generic Display Tool
      3. Instance Versus Class Attributes
      4. Name Considerations in Tool Classes
      5. Our Classes’ Final Form
    7. Step 7 (Final): Storing Objects in a Database
      1. Pickles and Shelves
      2. Storing Objects on a Shelve Database
      3. Exploring Shelves Interactively
      4. Updating Objects on a Shelve
    8. Future Directions
    9. Chapter Summary
    10. Test Your Knowledge: Quiz
    11. Test Your Knowledge: Answers
  36. 29. Class Coding Details
    1. The class Statement
      1. General Form
      2. Example
    2. Methods
      1. Method Example
      2. Calling Superclass Constructors
      3. Other Method Call Possibilities
    3. Inheritance
      1. Attribute Tree Construction
      2. Specializing Inherited Methods
      3. Class Interface Techniques
      4. Abstract Superclasses
    4. Namespaces: The Conclusion
      1. Simple Names: Global Unless Assigned
      2. Attribute Names: Object Namespaces
      3. The “Zen” of Namespaces: Assignments Classify Names
      4. Nested Classes: The LEGB Scopes Rule Revisited
      5. Namespace Dictionaries: Review
      6. Namespace Links: A Tree Climber
    5. Documentation Strings Revisited
    6. Classes Versus Modules
    7. Chapter Summary
    8. Test Your Knowledge: Quiz
    9. Test Your Knowledge: Answers
  37. 30. Operator Overloading
    1. The Basics
      1. Constructors and Expressions: __init__ and __sub__
      2. Common Operator Overloading Methods
    2. Indexing and Slicing: __getitem__ and __setitem__
      1. Intercepting Slices
      2. Slicing and Indexing in Python 2.X
      3. But 3.X’s __index__ Is Not Indexing!
    3. Index Iteration: __getitem__
    4. Iterable Objects: __iter__ and __next__
      1. User-Defined Iterables
      2. Multiple Iterators on One Object
      3. Coding Alternative: __iter__ plus yield
    5. Membership: __contains__, __iter__, and __getitem__
    6. Attribute Access: __getattr__ and __setattr__
      1. Attribute Reference
      2. Attribute Assignment and Deletion
      3. Other Attribute Management Tools
      4. Emulating Privacy for Instance Attributes: Part 1
    7. String Representation: __repr__ and __str__
      1. Why Two Display Methods?
      2. Display Usage Notes
    8. Right-Side and In-Place Uses: __radd__ and __iadd__
      1. Right-Side Addition
      2. In-Place Addition
    9. Call Expressions: __call__
      1. Function Interfaces and Callback-Based Code
    10. Comparisons: __lt__, __gt__, and Others
      1. The __cmp__ Method in Python 2.X
    11. Boolean Tests: __bool__ and __len__
      1. Boolean Methods in Python 2.X
    12. Object Destruction: __del__
      1. Destructor Usage Notes
    13. Chapter Summary
    14. Test Your Knowledge: Quiz
    15. Test Your Knowledge: Answers
  38. 31. Designing with Classes
    1. Python and OOP
      1. Polymorphism Means Interfaces, Not Call Signatures
    2. OOP and Inheritance: “Is-a” Relationships
    3. OOP and Composition: “Has-a” Relationships
      1. Stream Processors Revisited
    4. OOP and Delegation: “Wrapper” Proxy Objects
    5. Pseudoprivate Class Attributes
      1. Name Mangling Overview
      2. Why Use Pseudoprivate Attributes?
    6. Methods Are Objects: Bound or Unbound
      1. Unbound Methods Are Functions in 3.X
      2. Bound Methods and Other Callable Objects
    7. Classes Are Objects: Generic Object Factories
      1. Why Factories?
    8. Multiple Inheritance: “Mix-in” Classes
      1. Coding Mix-in Display Classes
    9. Other Design-Related Topics
    10. Chapter Summary
    11. Test Your Knowledge: Quiz
    12. Test Your Knowledge: Answers
  39. 32. Advanced Class Topics
    1. Extending Built-in Types
      1. Extending Types by Embedding
      2. Extending Types by Subclassing
    2. The “New Style” Class Model
      1. Just How New Is New-Style?
    3. New-Style Class Changes
      1. Attribute Fetch for Built-ins Skips Instances
      2. Type Model Changes
      3. All Classes Derive from “object”
      4. Diamond Inheritance Change
      5. More on the MRO: Method Resolution Order
      6. Example: Mapping Attributes to Inheritance Sources
    4. New-Style Class Extensions
      1. Slots: Attribute Declarations
      2. Properties: Attribute Accessors
      3. __getattribute__ and Descriptors: Attribute Tools
      4. Other Class Changes and Extensions
    5. Static and Class Methods
      1. Why the Special Methods?
      2. Static Methods in 2.X and 3.X
      3. Static Method Alternatives
      4. Using Static and Class Methods
      5. Counting Instances with Static Methods
      6. Counting Instances with Class Methods
    6. Decorators and Metaclasses: Part 1
      1. Function Decorator Basics
      2. A First Look at User-Defined Function Decorators
      3. A First Look at Class Decorators and Metaclasses
      4. For More Details
    7. The super Built-in Function: For Better or Worse?
      1. The Great super Debate
      2. Traditional Superclass Call Form: Portable, General
      3. Basic super Usage and Its Tradeoffs
      4. The super Upsides: Tree Changes and Dispatch
      5. Runtime Class Changes and super
      6. Cooperative Multiple Inheritance Method Dispatch
      7. The super Summary
    8. Class Gotchas
      1. Changing Class Attributes Can Have Side Effects
      2. Changing Mutable Class Attributes Can Have Side Effects, Too
      3. Multiple Inheritance: Order Matters
      4. Scopes in Methods and Classes
      5. Miscellaneous Class Gotchas
      6. KISS Revisited: “Overwrapping-itis”
    9. Chapter Summary
    10. Test Your Knowledge: Quiz
    11. Test Your Knowledge: Answers
    12. Test Your Knowledge: Part VI Exercises
  40. VII. Exceptions and Tools
  41. 33. Exception Basics
    1. Why Use Exceptions?
      1. Exception Roles
    2. Exceptions: The Short Story
      1. Default Exception Handler
      2. Catching Exceptions
      3. Raising Exceptions
      4. User-Defined Exceptions
      5. Termination Actions
    3. Chapter Summary
    4. Test Your Knowledge: Quiz
    5. Test Your Knowledge: Answers
  42. 34. Exception Coding Details
    1. The try/except/else Statement
      1. How try Statements Work
      2. try Statement Clauses
      3. The try else Clause
      4. Example: Default Behavior
      5. Example: Catching Built-in Exceptions
    2. The try/finally Statement
      1. Example: Coding Termination Actions with try/finally
    3. Unified try/except/finally
      1. Unified try Statement Syntax
      2. Combining finally and except by Nesting
      3. Unified try Example
    4. The raise Statement
      1. Raising Exceptions
      2. Scopes and try except Variables
      3. Propagating Exceptions with raise
      4. Python 3.X Exception Chaining: raise from
    5. The assert Statement
      1. Example: Trapping Constraints (but Not Errors!)
    6. with/as Context Managers
      1. Basic Usage
      2. The Context Management Protocol
      3. Multiple Context Managers in 3.1, 2.7, and Later
    7. Chapter Summary
    8. Test Your Knowledge: Quiz
    9. Test Your Knowledge: Answers
  43. 35. Exception Objects
    1. Exceptions: Back to the Future
      1. String Exceptions Are Right Out!
      2. Class-Based Exceptions
      3. Coding Exceptions Classes
    2. Why Exception Hierarchies?
    3. Built-in Exception Classes
      1. Built-in Exception Categories
      2. Default Printing and State
    4. Custom Print Displays
    5. Custom Data and Behavior
      1. Providing Exception Details
      2. Providing Exception Methods
    6. Chapter Summary
    7. Test Your Knowledge: Quiz
    8. Test Your Knowledge: Answers
  44. 36. Designing with Exceptions
    1. Nesting Exception Handlers
      1. Example: Control-Flow Nesting
      2. Example: Syntactic Nesting
    2. Exception Idioms
      1. Breaking Out of Multiple Nested Loops: “go to”
      2. Exceptions Aren’t Always Errors
      3. Functions Can Signal Conditions with raise
      4. Closing Files and Server Connections
      5. Debugging with Outer try Statements
      6. Running In-Process Tests
      7. More on sys.exc_info
      8. Displaying Errors and Tracebacks
    3. Exception Design Tips and Gotchas
      1. What Should Be Wrapped
      2. Catching Too Much: Avoid Empty except and Exception
      3. Catching Too Little: Use Class-Based Categories
    4. Core Language Summary
      1. The Python Toolset
      2. Development Tools for Larger Projects
    5. Chapter Summary
    6. Test Your Knowledge: Quiz
    7. Test Your Knowledge: Answers
    8. Test Your Knowledge: Part VII Exercises
  45. VIII. Advanced Topics
  46. 37. Unicode and Byte Strings
    1. String Changes in 3.X
    2. String Basics
      1. Character Encoding Schemes
      2. How Python Stores Strings in Memory
      3. Python’s String Types
      4. Text and Binary Files
    3. Coding Basic Strings
      1. Python 3.X String Literals
      2. Python 2.X String Literals
      3. String Type Conversions
    4. Coding Unicode Strings
      1. Coding ASCII Text
      2. Coding Non-ASCII Text
      3. Encoding and Decoding Non-ASCII text
      4. Other Encoding Schemes
      5. Byte String Literals: Encoded Text
      6. Converting Encodings
      7. Coding Unicode Strings in Python 2.X
      8. Source File Character Set Encoding Declarations
    5. Using 3.X bytes Objects
      1. Method Calls
      2. Sequence Operations
      3. Other Ways to Make bytes Objects
      4. Mixing String Types
    6. Using 3.X/2.6+ bytearray Objects
      1. bytearrays in Action
      2. Python 3.X String Types Summary
    7. Using Text and Binary Files
      1. Text File Basics
      2. Text and Binary Modes in 2.X and 3.X
      3. Type and Content Mismatches in 3.X
    8. Using Unicode Files
      1. Reading and Writing Unicode in 3.X
      2. Handling the BOM in 3.X
      3. Unicode Files in 2.X
      4. Unicode Filenames and Streams
    9. Other String Tool Changes in 3.X
      1. The re Pattern-Matching Module
      2. The struct Binary Data Module
      3. The pickle Object Serialization Module
      4. XML Parsing Tools
    10. Chapter Summary
    11. Test Your Knowledge: Quiz
    12. Test Your Knowledge: Answers
  47. 38. Managed Attributes
    1. Why Manage Attributes?
      1. Inserting Code to Run on Attribute Access
    2. Properties
      1. The Basics
      2. A First Example
      3. Computed Attributes
      4. Coding Properties with Decorators
    3. Descriptors
      1. The Basics
      2. A First Example
      3. Computed Attributes
      4. Using State Information in Descriptors
      5. How Properties and Descriptors Relate
    4. __getattr__ and __getattribute__
      1. The Basics
      2. A First Example
      3. Computed Attributes
      4. __getattr__ and __getattribute__ Compared
      5. Management Techniques Compared
      6. Intercepting Built-in Operation Attributes
    5. Example: Attribute Validations
      1. Using Properties to Validate
      2. Using Descriptors to Validate
      3. Using __getattr__ to Validate
      4. Using __getattribute__ to Validate
    6. Chapter Summary
    7. Test Your Knowledge: Quiz
      1. Test Your Knowledge: Answers
  48. 39. Decorators
    1. What’s a Decorator?
      1. Managing Calls and Instances
      2. Managing Functions and Classes
      3. Using and Defining Decorators
      4. Why Decorators?
    2. The Basics
      1. Function Decorators
      2. Class Decorators
      3. Decorator Nesting
      4. Decorator Arguments
      5. Decorators Manage Functions and Classes, Too
    3. Coding Function Decorators
      1. Tracing Calls
      2. Decorator State Retention Options
      3. Class Blunders I: Decorating Methods
      4. Timing Calls
      5. Adding Decorator Arguments
    4. Coding Class Decorators
      1. Singleton Classes
      2. Tracing Object Interfaces
      3. Class Blunders II: Retaining Multiple Instances
      4. Decorators Versus Manager Functions
      5. Why Decorators? (Revisited)
    5. Managing Functions and Classes Directly
    6. Example: “Private” and “Public” Attributes
      1. Implementing Private Attributes
      2. Implementation Details I
      3. Generalizing for Public Declarations, Too
      4. Implementation Details II
      5. Open Issues
      6. Python Isn’t About Control
    7. Example: Validating Function Arguments
      1. The Goal
      2. A Basic Range-Testing Decorator for Positional Arguments
      3. Generalizing for Keywords and Defaults, Too
      4. Implementation Details
      5. Open Issues
      6. Decorator Arguments Versus Function Annotations
      7. Other Applications: Type Testing (If You Insist!)
    8. Chapter Summary
    9. Test Your Knowledge: Quiz
    10. Test Your Knowledge: Answers
  49. 40. Metaclasses
    1. To Metaclass or Not to Metaclass
      1. Increasing Levels of “Magic”
      2. A Language of Hooks
      3. The Downside of “Helper” Functions
      4. Metaclasses Versus Class Decorators: Round 1
    2. The Metaclass Model
      1. Classes Are Instances of type
      2. Metaclasses Are Subclasses of Type
      3. Class Statement Protocol
    3. Declaring Metaclasses
      1. Declaration in 3.X
      2. Declaration in 2.X
      3. Metaclass Dispatch in Both 3.X and 2.X
    4. Coding Metaclasses
      1. A Basic Metaclass
      2. Customizing Construction and Initialization
      3. Other Metaclass Coding Techniques
    5. Inheritance and Instance
      1. Metaclass Versus Superclass
      2. Inheritance: The Full Story
    6. Metaclass Methods
      1. Metaclass Methods Versus Class Methods
      2. Operator Overloading in Metaclass Methods
    7. Example: Adding Methods to Classes
      1. Manual Augmentation
      2. Metaclass-Based Augmentation
      3. Metaclasses Versus Class Decorators: Round 2
    8. Example: Applying Decorators to Methods
      1. Tracing with Decoration Manually
      2. Tracing with Metaclasses and Decorators
      3. Applying Any Decorator to Methods
      4. Metaclasses Versus Class Decorators: Round 3 (and Last)
    9. Chapter Summary
    10. Test Your Knowledge: Quiz
    11. Test Your Knowledge: Answers
  50. 41. All Good Things
    1. The Python Paradox
      1. On “Optional” Language Features
      2. Against Disquieting Improvements
      3. Complexity Versus Power
      4. Simplicity Versus Elitism
      5. Closing Thoughts
    2. Where to Go From Here
    3. Encore: Print Your Own Completion Certificate!
  51. IX. Appendixes
  52. A. Installation and Configuration
    1. Installing the Python Interpreter
      1. Is Python Already Present?
      2. Where to Get Python
      3. Installation Steps
    2. Configuring Python
      1. Python Environment Variables
      2. How to Set Configuration Options
      3. Python Command-Line Arguments
      4. Python 3.3 Windows Launcher Command Lines
    3. For More Help
  53. B. The Python 3.3 Windows Launcher
    1. The Unix Legacy
    2. The Windows Legacy
    3. Introducing the New Windows Launcher
    4. A Windows Launcher Tutorial
      1. Step 1: Using Version Directives in Files
      2. Step 2: Using Command-Line Version Switches
      3. Step 3: Using and Changing Defaults
    5. Pitfalls of the New Windows Launcher
      1. Pitfall 1: Unrecognized Unix #! Lines Fail
      2. Pitfall 2: The Launcher Defaults to 2.X
      3. Pitfall 3: The New PATH Extension Option
    6. Conclusions: A Net Win for Windows
  54. C. Python Changes and This Book
    1. Major 2.X/3.X Differences
      1. 3.X Differences
      2. 3.X-Only Extensions
    2. General Remarks: 3.X Changes
      1. Changes in Libraries and Tools
      2. Migrating to 3.X
    3. Fifth Edition Python Changes: 2.7, 3.2, 3.3
      1. Changes in Python 2.7
      2. Changes in Python 3.3
      3. Changes in Python 3.2
    4. Fourth Edition Python Changes: 2.6, 3.0, 3.1
      1. Changes in Python 3.1
      2. Changes in Python 3.0 and 2.6
      3. Specific Language Removals in 3.0
    5. Third Edition Python Changes: 2.3, 2.4, 2.5
    6. Earlier and Later Python Changes
  55. D. Solutions to End-of-Part Exercises
    1. Part I, Getting Started
    2. Part II, Types and Operations
    3. Part III, Statements and Syntax
    4. Part IV, Functions and Generators
    5. Part V, Modules and Packages
    6. Part VI, Classes and OOP
    7. Part VII, Exceptions and Tools
  56. Index
  57. About the Author

Product information

  • Title: Learning Python, 5th Edition
  • Author(s): Mark Lutz
  • Release date: June 2013
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449355739