Cover image for Learning Python, 4th Edition

Book description

Google and YouTube use Python because it's highly adaptable, easy to maintain, and allows for rapid development. If you want to write high-quality, efficient code that's easily integrated with other languages and tools, this hands-on book will help you be productive with Python quickly -- whether you're new to programming or just new to Python. It's an easy-to-follow self-paced tutorial, based on author and Python expert Mark Lutz's popular training course. Each chapter contains a stand-alone lesson on a key component of the language, and includes a unique Test Your Knowledge section with practical exercises and quizzes, so you can practice new skills and test your understanding as you go. You'll find lots of annotated examples and illustrations to help you get started with Python 3.0.

  • Learn about Python's major built-in object types, such as numbers, lists, and dictionaries

  • Create and process objects using Python statements, and learn Python's general syntax model

  • Structure and reuse code using functions, Python's basic procedural tool

  • Learn about Python modules: packages of statements, functions, and other tools, organized into larger components

  • Discover Python's object-oriented programming tool for structuring code

  • Learn about the exception-handling model, and development tools for writing larger programs

  • Explore advanced Python tools including decorators, descriptors, metaclasses, and Unicode processing

Table of Contents

  1. Learning Python
  2. Dedication
  3. A Note Regarding Supplemental Files
  4. Preface
    1. About This Fourth Edition
      1. Coverage for Both 3.0 and 2.6
      2. New Chapters
      3. Changes to Existing Material
      4. Specific Language Extensions in 2.6 and 3.0
      5. Specific Language Removals in 3.0
    2. About The Third Edition
      1. The Third Edition’s Python Language Changes
      2. The Third Edition’s Python Training Changes
      3. The Third Edition’s Structural Changes
      4. The Third Edition’s Scope Changes
    3. About This Book
      1. This Book’s Prerequisites
      2. This Book’s Scope and Other Books
      3. This Book’s Style and Structure
    4. Book Updates
    5. About the Programs in This Book
    6. Using Code Examples
    7. Font Conventions
    8. Safari® Books Online
    9. How to Contact Us
    10. Acknowledgments
  5. I. Getting Started
    1. 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. Gaming, Images, Serial Ports, XML, Robots, and More
      6. How Is Python Supported?
      7. What Are Python’s Technical Strengths?
        1. It’s Object-Oriented
        2. It’s Free
        3. It’s Portable
        4. It’s Powerful
        5. It’s Mixable
        6. It’s Easy to Use
        7. It’s 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
    2. 2. How Python Runs Programs
      1. Introducing the Python Interpreter
      2. Program Execution
        1. The Programmer’s View
        2. Python’s View
          1. Byte code compilation
          2. The Python Virtual Machine (PVM)
          3. Performance implications
          4. Development implications
      3. Execution Model Variations
        1. Python Implementation Alternatives
          1. CPython
          2. Jython
          3. IronPython
        2. Execution Optimization Tools
          1. The Psyco just-in-time compiler
          2. The Shedskin C++ translator
        3. Frozen Binaries
        4. Other Execution Options
        5. Future Possibilities?
      4. Chapter Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Answers
    3. 3. How You Run Programs
      1. The Interactive Prompt
        1. Running Code Interactively
        2. Why the Interactive Prompt?
          1. Experimenting
          2. Testing
        3. Using the Interactive Prompt
          1. Entering multiline statements
      2. System Command Lines and Files
        1. A First Script
        2. Running Files with Command Lines
        3. Using Command Lines and Files
        4. Unix Executable Scripts (#!)
      3. Clicking File Icons
        1. Clicking Icons on Windows
        2. The input Trick
        3. Other Icon-Click Limitations
      4. Module Imports and Reloads
        1. The Grander Module Story: Attributes
          1. Modules and namespaces
        2. import and reload Usage Notes
      5. Using exec to Run Module Files
      6. The IDLE User Interface
        1. IDLE Basics
        2. Using IDLE
        3. Advanced IDLE Tools
      7. Other IDEs
      8. Other Launch Options
        1. Embedding Calls
        2. Frozen Binary Executables
        3. Text Editor Launch Options
        4. Still Other Launch Options
        5. Future Possibilities?
      9. Which Option Should I Use?
      10. Chapter Summary
      11. Test Your Knowledge: Quiz
      12. Test Your Knowledge: Answers
      13. Test Your Knowledge: Part I Exercises
  6. II. Types and Operations
    1. 4. Introducing Python Object Types
      1. Why Use Built-in Types?
        1. Python’s Core Data Types
      2. Numbers
      3. Strings
        1. Sequence Operations
        2. Immutability
        3. Type-Specific Methods
        4. Getting Help
        5. Other Ways to Code Strings
        6. Pattern Matching
      4. Lists
        1. Sequence Operations
        2. Type-Specific Operations
        3. Bounds Checking
        4. Nesting
        5. Comprehensions
      5. Dictionaries
        1. Mapping Operations
        2. Nesting Revisited
        3. Sorting Keys: for Loops
        4. Iteration and Optimization
        5. Missing Keys: if Tests
      6. Tuples
        1. Why Tuples?
      7. Files
        1. Other File-Like Tools
      8. Other Core Types
        1. How to Break Your Code’s Flexibility
        2. User-Defined Classes
        3. And Everything Else
      9. Chapter Summary
      10. Test Your Knowledge: Quiz
      11. Test Your Knowledge: Answers
    2. 5. Numeric Types
      1. Numeric Type Basics
        1. Numeric Literals
        2. Built-in Numeric Tools
        3. Python Expression Operators
          1. Mixed operators follow operator precedence
          2. Parentheses group subexpressions
          3. Mixed types are converted up
          4. Preview: Operator overloading and polymorphism
      2. Numbers in Action
        1. Variables and Basic Expressions
        2. Numeric Display Formats
        3. Comparisons: Normal and Chained
        4. Division: Classic, Floor, and True
          1. Supporting either Python
          2. Floor versus truncation
          3. Why does truncation matter?
        5. Integer Precision
        6. Complex Numbers
        7. Hexadecimal, Octal, and Binary Notation
        8. Bitwise Operations
        9. Other Built-in Numeric Tools
      3. Other Numeric Types
        1. Decimal Type
          1. The basics
          2. Setting precision globally
          3. Decimal context manager
        2. Fraction Type
          1. The basics
          2. Numeric accuracy
          3. Conversions and mixed types
        3. Sets
          1. Set basics in Python 2.6
          2. Set literals in Python 3.0
          3. Immutable constraints and frozen sets
          4. Set comprehensions in Python 3.0
          5. Why sets?
        4. Booleans
      4. Numeric Extensions
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    3. 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
    4. 7. Strings
      1. String Literals
        1. Single- and Double-Quoted Strings Are the Same
        2. Escape Sequences Represent Special Bytes
        3. Raw Strings Suppress Escapes
        4. Triple Quotes Code Multiline Block Strings
      2. Strings in Action
        1. Basic Operations
        2. Indexing and Slicing
          1. Extended slicing: the third limit and slice objects
        3. String Conversion Tools
          1. Character code conversions
        4. Changing Strings
      3. String Methods
        1. String Method Examples: Changing Strings
        2. String Method Examples: Parsing Text
        3. Other Common String Methods in Action
        4. The Original string Module (Gone in 3.0)
      4. String Formatting Expressions
        1. Advanced String Formatting Expressions
        2. Dictionary-Based String Formatting Expressions
      5. String Formatting Method Calls
        1. The Basics
        2. Adding Keys, Attributes, and Offsets
        3. Adding Specific Formatting
        4. Comparison to the % Formatting Expression
        5. Why the New Format Method?
          1. Extra features
          2. Explicit value references
          3. Method names and general arguments
          4. Possible future deprecation?
      6. General Type Categories
        1. Types Share Operation Sets by Categories
        2. Mutable Types Can Be Changed In-Place
      7. Chapter Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Answers
    5. 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
          1. Index and slice assignments
          2. List method calls
          3. Other common list operations
      3. Dictionaries
      4. Dictionaries in Action
        1. Basic Dictionary Operations
        2. Changing Dictionaries In-Place
        3. More Dictionary Methods
        4. A Languages Table
        5. Dictionary Usage Notes
          1. Using dictionaries to simulate flexible lists
          2. Using dictionaries for sparse data structures
          3. Avoiding missing-key errors
          4. Using dictionaries as “records”
        6. Other Ways to Make Dictionaries
        7. Dictionary Changes in Python 3.0
          1. Dictionary comprehensions
          2. Dictionary views
          3. Dictionary views and sets
          4. Sorting dictionary keys
          5. Dictionary magnitude comparisons no longer work
          6. The has_key method is dead: long live in!
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    6. 9. Tuples, Files, and Everything Else
      1. Tuples
        1. Tuples in Action
          1. Tuple syntax peculiarities: Commas and parentheses
          2. Conversions, methods, and immutability
        2. Why Lists and Tuples?
      2. Files
        1. Opening Files
        2. Using Files
        3. Files in Action
          1. Text and binary files in Python 3.0
          2. Storing and parsing Python objects in files
          3. Storing native Python objects with pickle
          4. Storing and parsing packed binary data in files
          5. File context managers
        4. Other File Tools
      3. Type Categories Revisited
      4. Object Flexibility
      5. References Versus Copies
      6. Comparisons, Equality, and Truth
        1. Python 3.0 Dictionary Comparisons
        2. The Meaning of True and False in Python
          1. The None object
          2. The bool type
      7. Python’s Type Hierarchies
        1. Type Objects
      8. Other Types in Python
      9. 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
      10. Chapter Summary
      11. Test Your Knowledge: Quiz
      12. Test Your Knowledge: Answers
      13. Test Your Knowledge: Part II Exercises
  7. III. Statements and Syntax
    1. 10. Introducing Python Statements
      1. Python Program Structure Revisited
        1. Python’s Statements
      2. A Tale of Two ifs
        1. What Python Adds
        2. What Python Removes
          1. Parentheses are optional
          2. End of line is end of statement
          3. End of indentation is end of block
        3. Why Indentation Syntax?
        4. A Few Special Cases
          1. Statement rule special cases
          2. Block rule special case
      3. 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
      4. Chapter Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Answers
    2. 11. Assignments, Expressions, and Prints
      1. Assignment Statements
        1. Assignment Statement Forms
        2. Sequence Assignments
          1. Advanced sequence assignment patterns
        3. Extended Sequence Unpacking in Python 3.0
          1. Extended unpacking in action
          2. Boundary cases
          3. A useful convenience
          4. Application to for loops
        4. Multiple-Target Assignments
          1. Multiple-target assignment and shared references
        5. Augmented Assignments
          1. Augmented assignment and shared references
        6. Variable Name Rules
          1. Naming conventions
          2. Names have no type, but objects do
      2. Expression Statements
        1. Expression Statements and In-Place Changes
      3. Print Operations
        1. The Python 3.0 print Function
          1. Call format
          2. The 3.0 print function in action
        2. The Python 2.6 print Statement
          1. Statement forms
          2. The 2.6 print statement in action
        3. Print Stream Redirection
          1. The Python “hello world” program
          2. Manual stream redirection
          3. Automatic stream redirection
        4. Version-Neutral Printing
      4. Chapter Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Answers
    3. 12. if Tests and Syntax Rules
      1. if Statements
        1. General Format
        2. Basic Examples
        3. Multiway Branching
      2. Python Syntax Rules
        1. Block Delimiters: Indentation Rules
          1. Avoid mixing tabs and spaces: New error checking in 3.0
        2. Statement Delimiters: Lines and Continuations
        3. A Few Special Cases
      3. Truth Tests
      4. The if/else Ternary Expression
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    4. 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
          1. More on the loop else
      3. for Loops
        1. General Format
        2. Examples
          1. Basic usage
          2. Other data types
          3. Tuple assignment in for loops
          4. Python 3.0 extended sequence assignment in for loops
          5. Nested for loops
      4. Loop Coding Techniques
        1. Counter Loops: while and range
        2. Nonexhaustive Traversals: range and Slices
        3. Changing Lists: range
        4. Parallel Traversals: zip and map
          1. map equivalence in Python 2.6
          2. Dictionary construction with zip
        5. Generating Both Offsets and Items: enumerate
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    5. 14. Iterations and Comprehensions, Part 1
      1. Iterators: A First Look
        1. The Iteration Protocol: File Iterators
        2. Manual Iteration: iter and next
        3. Other Built-in Type Iterators
      2. List Comprehensions: A First 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.0
        1. The range Iterator
        2. The map, zip, and filter Iterators
        3. Multiple Versus Single Iterators
        4. Dictionary View Iterators
      5. Other Iterator Topics
      6. Chapter Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Answers
    6. 15. The Documentation Interlude
      1. Python Documentation Sources
        1. # Comments
        2. The dir Function
        3. Docstrings: __doc__
          1. User-defined docstrings
          2. Docstring standards
          3. Built-in docstrings
        4. PyDoc: The help Function
        5. PyDoc: HTML Reports
        6. The Standard Manual Set
        7. Web Resources
        8. 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
  8. IV. Functions
    1. 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
    2. 17. Scopes
      1. Python Scope Basics
        1. Scope Rules
        2. Name Resolution: The LEGB Rule
        3. Scope Example
        4. The Built-in Scope
      2. The global Statement
        1. Minimize Global Variables
        2. Minimize Cross-File Changes
        3. Other Ways to Access Globals
      3. Scopes and Nested Functions
        1. Nested Scope Details
        2. Nested Scope Examples
          1. Factory functions
          2. Retaining enclosing scopes’ state with defaults
          3. Nested scopes and lambdas
          4. Scopes versus defaults with loop variables
          5. Arbitrary scope nesting
      4. The nonlocal Statement
        1. nonlocal Basics
        2. nonlocal in Action
          1. Using nonlocal for changes
          2. Boundary cases
        3. Why nonlocal?
          1. Shared state with globals
          2. State with classes (preview)
          3. State with function attributes
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    3. 18. Arguments
      1. Argument-Passing Basics
        1. Arguments and Shared References
        2. Avoiding Mutable Argument Changes
        3. Simulating Output Parameters
      2. Special Argument-Matching Modes
        1. The Basics
        2. Matching Syntax
        3. The Gritty Details
        4. Keyword and Default Examples
          1. Keywords
          2. Defaults
          3. Combining keywords and defaults
        5. Arbitrary Arguments Examples
          1. Collecting arguments
          2. Unpacking arguments
          3. Applying functions generically
          4. The defunct apply built-in (Python 2.6)
        6. Python 3.0 Keyword-Only Arguments
          1. Ordering rules
          2. Why 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.0 print Function
        1. Using Keyword-Only Arguments
      6. Chapter Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Answers
    4. 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
        2. Function Introspection
        3. Function Attributes
        4. Function Annotations in 3.0
      4. Anonymous Functions: lambda
        1. lambda Basics
        2. Why Use lambda?
        3. How (Not) to Obfuscate Your Python Code
        4. Nested lambdas and Scopes
      5. Mapping Functions over Sequences: map
      6. Functional Programming Tools: filter and reduce
      7. Chapter Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Answers
    5. 20. Iterations and Comprehensions, Part 2
      1. List Comprehensions Revisited: Functional Tools
        1. List Comprehensions Versus map
        2. Adding Tests and Nested Loops: filter
        3. List Comprehensions and Matrixes
        4. Comprehending List Comprehensions
      2. Iterators Revisited: Generators
        1. Generator Functions: yield Versus return
          1. State suspension
          2. Iteration protocol integration
          3. Generator functions in action
          4. Extended generator function protocol: send versus next
        2. Generator Expressions: Iterators Meet Comprehensions
        3. Generator Functions Versus Generator Expressions
        4. Generators Are Single-Iterator Objects
        5. Emulating zip and map with Iteration Tools
          1. Coding your own map(func, ...)
          2. Coding your own zip(...) and map(None, ...)
        6. Value Generation in Built-in Types and Classes
      3. 3.0 Comprehension Syntax Summary
        1. Comprehending Set and Dictionary Comprehensions
        2. Extended Comprehension Syntax for Sets and Dictionaries
      4. Timing Iteration Alternatives
        1. Timing Module
        2. Timing Script
        3. Timing Results
        4. Timing Module Alternatives
          1. Using keyword-only arguments in 3.0
        5. Other Suggestions
      5. Function Gotchas
        1. Local Names Are Detected Statically
        2. Defaults and Mutable Objects
        3. Functions Without returns
        4. Enclosing Scope Loop Variables
      6. Chapter Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Answers
      9. Test Your Knowledge: Part IV Exercises
  9. V. Modules
    1. 21. 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. The Module Search Path
        1. Configuring the Search Path
        2. Search Path Variations
        3. The sys.path List
        4. Module File Selection
        5. Advanced Module Selection Concepts
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    2. 22. Module Coding Basics
      1. Module Creation
      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. Cross-File Name Changes
        7. import and from Equivalence
        8. Potential Pitfalls of the from Statement
          1. When import is required
      3. Module Namespaces
        1. Files Generate Namespaces
        2. Attribute Name Qualification
        3. Imports Versus Scopes
        4. Namespace Nesting
      4. Reloading Modules
        1. reload Basics
        2. reload Example
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    3. 23. 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.0
        2. Relative Import Basics
        3. Why Relative Imports?
          1. The relative imports solution in 3.0
          2. Relative imports versus absolute package paths
        4. The Scope of Relative Imports
        5. Module Lookup Rules Summary
        6. Relative Imports in Action
          1. Imports outside packages
          2. Imports within packages
          3. Imports are still relative to the CWD
          4. Selecting modules with relative and absolute imports
          5. Imports are still relative to the CWD (again)
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    4. 24. Advanced Module Topics
      1. Data Hiding in Modules
        1. Minimizing from * Damage: _X and __all__
      2. Enabling Future Language Features
      3. Mixed Usage Modes: __name__ and __main__
        1. Unit Tests with __name__
        2. Using Command-Line Arguments with __name__
      4. Changing the Module Search Path
      5. The as Extension for import and from
      6. Modules Are Objects: Metaprograms
      7. Importing Modules by Name String
      8. Transitive Module Reloads
      9. Module Design Concepts
      10. Module Gotchas
        1. Statement Order Matters in Top-Level Code
        2. from Copies Names but Doesn’t Link
        3. from * Can Obscure the Meaning of Variables
        4. reload May Not Impact from Imports
        5. reload, from, and Interactive Testing
        6. Recursive from Imports May Not Work
      11. Chapter Summary
      12. Test Your Knowledge: Quiz
      13. Test Your Knowledge: Answers
      14. Test Your Knowledge: Part V Exercises
  10. VI. Classes and OOP
    1. 25. OOP: The Big Picture
      1. Why Use Classes?
      2. OOP from 30,000 Feet
        1. Attribute Inheritance Search
        2. Classes and Instances
        3. Class Method Calls
        4. Coding Class Trees
        5. OOP Is About Code Reuse
      3. Chapter Summary
      4. Test Your Knowledge: Quiz
      5. Test Your Knowledge: Answers
    2. 26. 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. Classes Versus Dictionaries
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    3. 27. 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
    4. 28. 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
        5. Python 2.6 and 3.0 Abstract Superclasses
      4. Namespaces: The Whole Story
        1. Simple Names: Global Unless Assigned
        2. Attribute Names: Object Namespaces
        3. The “Zen” of Python Namespaces: Assignments Classify Names
        4. Namespace Dictionaries
        5. Namespace Links
      5. Documentation Strings Revisited
      6. Classes Versus Modules
      7. Chapter Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Answers
    5. 29. 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
      3. Index Iteration: __getitem__
      4. Iterator Objects: __iter__ and __next__
        1. User-Defined Iterators
        2. Multiple Iterators on One Object
      5. Membership: __contains__, __iter__, and __getitem__
      6. Attribute Reference: __getattr__ and __setattr__
        1. Other Attribute Management Tools
        2. Emulating Privacy for Instance Attributes: Part 1
      7. String Representation: __repr__ and __str__
      8. Right-Side and In-Place Addition: __radd__ and __iadd__
        1. In-Place Addition
      9. Call Expressions: __call__
        1. Function Interfaces and Callback-Based Code
      10. Comparisons: __lt__, __gt__, and Others
        1. The 2.6 __cmp__ Method (Removed in 3.0)
      11. Boolean Tests: __bool__ and __len__
      12. Object Destruction: __del__
      13. Chapter Summary
      14. Test Your Knowledge: Quiz
      15. Test Your Knowledge: Answers
    6. 30. Designing with Classes
      1. Python and OOP
        1. Overloading by Call Signatures (or Not)
      2. OOP and Inheritance: “Is-a” Relationships
      3. OOP and Composition: “Has-a” Relationships
        1. Stream Processors Revisited
      4. OOP and Delegation: “Wrapper” 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.0
        2. Bound Methods and Other Callable Objects
      7. Multiple Inheritance: “Mix-in” Classes
        1. Coding Mix-in Display Classes
          1. Listing instance attributes with __dict__
          2. Listing inherited attributes with dir
          3. Listing attributes per object in class trees
      8. Classes Are Objects: Generic Object Factories
        1. Why Factories?
      9. Other Design-Related Topics
      10. Chapter Summary
      11. Test Your Knowledge: Quiz
      12. Test Your Knowledge: Answers
    7. 31. Advanced Class Topics
      1. Extending Built-in Types
        1. Extending Types by Embedding
        2. Extending Types by Subclassing
      2. The “New-Style” Class Model
      3. New-Style Class Changes
        1. Type Model Changes
          1. Implications for type testing
          2. All objects derive from “object”
        2. Diamond Inheritance Change
          1. Diamond inheritance example
          2. Explicit conflict resolution
          3. Scope of search order change
      4. New-Style Class Extensions
        1. Instance Slots
          1. Slots and generic code
          2. Multiple __slot__ lists in superclasses
        2. Class Properties
        3. __getattribute__ and Descriptors
        4. Metaclasses
      5. Static and Class Methods
        1. Why the Special Methods?
        2. Static Methods in 2.6 and 3.0
        3. Static Method Alternatives
        4. Using Static and Class Methods
        5. Counting Instances with Static Methods
        6. Counting Instances with Class Methods
          1. Counting instances per class with class methods
      6. Decorators and Metaclasses: Part 1
        1. Function Decorator Basics
        2. A First Function Decorator Example
        3. Class Decorators and Metaclasses
        4. For More Details
      7. 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. Methods, Classes, and Nested Scopes
        5. Delegation-Based Classes in 3.0: __getattr__ and built-ins
        6. “Overwrapping-itis”
      8. Chapter Summary
      9. Test Your Knowledge: Quiz
      10. Test Your Knowledge: Answers
      11. Test Your Knowledge: Part VI Exercises
  11. VII. Exceptions and Tools
    1. 32. 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
    2. 33. Exception Coding Details
      1. The try/except/else Statement
        1. try Statement Clauses
        2. The try else Clause
        3. Example: Default Behavior
        4. 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. Propagating Exceptions with raise
        2. Python 3.0 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
      7. Chapter Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Answers
    3. 34. 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
    4. 35. Designing with Exceptions
      1. Nesting Exception Handlers
        1. Example: Control-Flow Nesting
        2. Example: Syntactic Nesting
      2. Exception Idioms
        1. Exceptions Aren’t Always Errors
        2. Functions Can Signal Conditions with raise
        3. Closing Files and Server Connections
        4. Debugging with Outer try Statements
        5. Running In-Process Tests
        6. More on sys.exc_info
      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
  12. VIII. Advanced Topics
    1. 36. Unicode and Byte Strings
      1. String Changes in 3.0
      2. String Basics
        1. Character Encoding Schemes
        2. Python’s String Types
        3. Text and Binary Files
      3. Python 3.0 Strings in Action
        1. Literals and Basic Properties
        2. Conversions
      4. Coding Unicode Strings
        1. Coding ASCII Text
        2. Coding Non-ASCII Text
        3. Encoding and Decoding Non-ASCII text
        4. Other Unicode Coding Techniques
        5. Converting Encodings
        6. Coding Unicode Strings in Python 2.6
        7. Source File Character Set Encoding Declarations
      5. Using 3.0 Bytes Objects
        1. Method Calls
        2. Sequence Operations
        3. Other Ways to Make bytes Objects
        4. Mixing String Types
      6. Using 3.0 (and 2.6) bytearray Objects
      7. Using Text and Binary Files
        1. Text File Basics
        2. Text and Binary Modes in 3.0
        3. Type and Content Mismatches
      8. Using Unicode Files
        1. Reading and Writing Unicode in 3.0
          1. Manual encoding
          2. File output encoding
          3. File input decoding
          4. Decoding mismatches
        2. Handling the BOM in 3.0
        3. Unicode Files in 2.6
      9. Other String Tool Changes in 3.0
        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
    2. 37. 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
          1. Descriptor method arguments
          2. Read-only descriptors
        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
          1. Avoiding loops in attribute interception methods
        2. A First Example
        3. Computed Attributes
        4. __getattr__ and __getattribute__ Compared
        5. Management Techniques Compared
        6. Intercepting Built-in Operation Attributes
        7. Delegation-Based Managers Revisited
      5. Example: Attribute Validations
        1. Using Properties to Validate
          1. Self-test code
        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
    3. 38. 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
          1. Usage
          2. Implementation
          3. Supporting method decoration
        2. Class Decorators
          1. Usage
          2. Implementation
          3. Supporting multiple instances
        3. Decorator Nesting
        4. Decorator Arguments
        5. Decorators Manage Functions and Classes, Too
      3. Coding Function Decorators
        1. Tracing Calls
        2. State Information Retention Options
          1. Class instance attributes
          2. Enclosing scopes and globals
          3. Enclosing scopes and nonlocals
          4. Function attributes
        3. Class Blunders I: Decorating Class Methods
          1. Using nested functions to decorate methods
          2. Using descriptors to decorate 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
          1. Inheritance versus delegation
          2. Decorator arguments
          3. State retention and enclosing scopes
          4. Using __dict__ and __slots__
        3. Generalizing for Public Declarations, Too
        4. Implementation Details II
          1. Using __X pseudoprivate names
          2. Breaking privacy
          3. Decorator tradeoffs
        5. Open Issues
          1. Caveat: operator overloading methods fail to delegate under 3.0
          2. Implementation alternatives: __getattribute__ inserts, call stack inspection
        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
          1. Function introspection
          2. Argument assumptions
          3. Matching algorithm
        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
    4. 39. Metaclasses
      1. To Metaclass or Not to Metaclass
        1. Increasing Levels of Magic
        2. The Downside of “Helper” Functions
        3. 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
      4. Coding Metaclasses
        1. A Basic Metaclass
        2. Customizing Construction and Initialization
        3. Other Metaclass Coding Techniques
          1. Using simple factory functions
          2. Overloading class creation calls with metaclasses
          3. Overloading class creation calls with normal classes
        4. Instances Versus Inheritance
      5. Example: Adding Methods to Classes
        1. Manual Augmentation
        2. Metaclass-Based Augmentation
        3. Metaclasses Versus Class Decorators: Round 2
          1. Decorator-based augmentation
          2. Managing instances instead of classes
      6. 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
      7. Chapter Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Answers
  13. IX. Appendixes
    1. 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
          1. Unix/Linux shell variables
          2. DOS variables (Windows)
          3. Windows environment variable GUI
          4. Windows registry
          5. Path files
        3. Python Command-Line Options
      3. For More Help
    2. B. 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
      5. Part V, Modules
      6. Part VI, Classes and OOP
      7. Part VII, Exceptions and Tools
  14. Index
  15. About the Author
  16. Colophon
  17. Copyright