You are previewing Learning Python, 5th Edition.
O'Reilly logo
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.

Table of Contents

  1. Learning Python
  2. Dedication
  3. A Note Regarding Supplemental Files
  4. 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
  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. 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
    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: The standard
          2. Jython: Python for Java
          3. IronPython: Python for .NET
          4. Stackless: Python for concurrency
          5. PyPy: Python for speed
        2. Execution Optimization Tools
          1. Cython: A Python/C hybrid
          2. Shed Skin: A Python-to-C++ translator
          3. Psyco: The original just-in-time compiler
        3. Frozen Binaries
        4. 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. 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?
          1. Experimenting
          2. Testing
        8. Usage Notes: The Interactive Prompt
          1. Entering multiline statements
      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
          1. Modules and namespaces
        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
    1. 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
    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. Hex, Octal, Binary: Literals and Conversions
        8. Bitwise Operations
        9. Other Built-in Numeric Tools
      3. Other Numeric Types
        1. Decimal Type
          1. Decimal basics
          2. Setting decimal precision globally
          3. Decimal context manager
        2. Fraction Type
          1. Fraction basics
          2. Numeric accuracy in fractions and decimals
          3. Fraction conversions and mixed types
        3. Sets
          1. Set basics in Python 2.6 and earlier
          2. Set literals in Python 3.X and 2.7
          3. Immutable constraints and frozen sets
          4. Set comprehensions in Python 3.X and 2.7
          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. 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
          1. Extended slicing: The third limit and slice objects
        3. String Conversion Tools
          1. Character code conversions
        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?
          1. Extra features: Special-case “batteries” versus general techniques
          2. Flexible reference syntax: Extra complexity and functional overlap
          3. Explicit value references: Now optional and unlikely to be used
          4. Named method and context-neutral arguments: Aesthetics versus practice
          5. Functions versus expressions: A minor convenience
      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
    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. More on sorting lists
          4. Other common list methods
          5. Other common list operations
      3. Dictionaries
      4. Dictionaries in Action
        1. Basic Dictionary Operations
        2. Changing Dictionaries in Place
        3. More Dictionary Methods
        4. Example: Movie Database
          1. Preview: Mapping values to keys
        5. Dictionary Usage Notes
          1. Using dictionaries to simulate flexible lists: Integer keys
          2. Using dictionaries for sparse data structures: Tuple keys
          3. Avoiding missing-key errors
          4. Nesting in dictionaries
        6. Other Ways to Make Dictionaries
        7. Dictionary Changes in Python 3.X and 2.7
          1. Dictionary comprehensions in 3.X and 2.7
          2. Dictionary views in 3.X (and 2.7 via new methods)
          3. Dictionary views and sets
          4. Sorting dictionary keys in 3.X
          5. Dictionary magnitude comparisons no longer work in 3.X
          6. The has_key method is dead in 3.X: 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?
        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
          1. Python 2.X and 3.X mixed-type comparisons and sorts
          2. Python 2.X and 3.X dictionary comparisons
        4. The Meaning of True and False in Python
          1. The None object
          2. The bool type
        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
  7. III. Statements and Syntax
    1. 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
          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
      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
          1. Supporting floating-point numbers
        5. Nesting Code Three Levels Deep
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. 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.X
          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.X print Function
          1. Call format
          2. The 3.X print function in action
        2. The Python 2.X print Statement
          1. Statement forms
          2. The 2.X 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
          1. 2to3 converter
          2. Importing from __future__
          3. Neutralizing display differences with code
      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
          1. Handling switch defaults
          2. Handling larger actions
      2. Python Syntax Revisited
        1. Block Delimiters: Indentation Rules
          1. Avoid mixing tabs and spaces: New error checking in 3.X
        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
    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.X extended sequence assignment in for loops
          5. Nested for loops
      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
          1. map equivalence in Python 2.X
          2. Dictionary construction with zip
        7. Generating Both Offsets and Items: enumerate
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
    5. 14. Iterations and Comprehensions
      1. Iterations: A First Look
        1. The Iteration Protocol: File Iterators
        2. Manual Iteration: iter and next
          1. The full iteration protocol
          2. Manual iteration
        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
          1. Filter clauses: if
          2. Nested loops: for
      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
    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 and priorities
          3. Built-in docstrings
        4. PyDoc: The help Function
        5. PyDoc: HTML Reports
          1. Python 3.2 and later: PyDoc’s all-browser mode
          2. Python 3.2 and earlier: GUI client
        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
  8. IV. Functions and Generators
    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 Details
        2. Name Resolution: The LEGB Rule
          1. Other Python scopes: Preview
        3. Scope Example
        4. The Built-in Scope
          1. Redefining built-in names: For better or worse
      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
          1. A simple function factory
          2. Closures versus classes, round 1
        4. Retaining Enclosing Scope State with Defaults
          1. Nested scopes, defaults, and lambdas
          2. Loop variables may require defaults, not scopes
          3. Arbitrary scope nesting
      4. The nonlocal Statement in 3.X
        1. nonlocal Basics
        2. nonlocal in Action
          1. Using nonlocal for changes
          2. Boundary cases
      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
          1. State with mutables: Obscure ghost of Pythons past?
      6. Chapter Summary
      7. Test Your Knowledge: Quiz
      8. 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 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
          1. Keywords
          2. Defaults
          3. Combining keywords and defaults
        5. Arbitrary Arguments Examples
          1. Headers: Collecting arguments
          2. Calls: Unpacking arguments
          3. Applying functions generically
          4. The defunct apply built-in (Python 2.X)
        6. Python 3.X 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.X 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
          1. Recursion versus queues and stacks
          2. Cycles, paths, and stack limits
          3. More recursion examples
      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?
          1. Multiway branch switches: The finale
        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
    5. 20. Comprehensions and Generations
      1. List Comprehensions and Functional Tools
        1. List Comprehensions Versus map
        2. Adding Tests and Nested Loops: filter
          1. Formal comprehension syntax
        3. Example: List Comprehensions and Matrixes
        4. Don’t Abuse List Comprehensions: KISS
          1. On the other hand: performance, conciseness, expressiveness
      2. Generator Functions and Expressions
        1. Generator Functions: yield Versus return
          1. State suspension
          2. Iteration protocol integration
          3. Generator functions in action
          4. Why generator functions?
          5. Extended generator function protocol: send versus next
        2. Generator Expressions: Iterables Meet Comprehensions
          1. Why generator expressions?
          2. Generator expressions versus map
          3. Generator expressions versus filter
        3. Generator Functions Versus Generator Expressions
        4. Generators Are Single-Iteration Objects
        5. Generation in Built-in Types, Tools, and Classes
          1. Generators and library tools: Directory walkers
          2. Generators and function application
          3. Preview: User-defined iterables in classes
        6. Example: Generating Scrambled Sequences
          1. Scrambling sequences
          2. Simple functions
          3. Generator functions
          4. Generator expressions
          5. Tester client
          6. Permutations: All possible combinations
        7. Don’t Abuse Generators: EIBTI
          1. On the other hand: Space and time, conciseness, expressiveness
        8. Example: Emulating zip and map with Iteration Tools
          1. Coding your own map(func, ...)
          2. Coding your own zip(...) and map(None, ...)
      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
    6. 21. The Benchmarking Interlude
      1. Timing Iteration Alternatives
        1. Timing Module: Homegrown
        2. Timing Script
        3. Timing Results
          1. The impact of function calls: map
        4. Timing Module Alternatives
          1. Using keyword-only arguments in 3.X
        5. Other Suggestions
      2. Timing Iterations and Pythons with timeit
        1. Basic timeit Usage
          1. Interactive usage and API calls
          2. Command-line usage
          3. Timing multiline statements
          4. Other usage modes: Setup, totals, and objects
        2. Benchmark Module and Script: timeit
        3. Benchmark Script Results
        4. More Fun with Benchmarks
          1. A win for map and a rare loss for PyPy
          2. The impact of function calls revisited
          3. Comparing techniques: Homegrown versus batteries
          4. Room for improvement: Setup
      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
          1. Enclosing scopes and loop variables: Factory functions
          2. Hiding built-ins by assignment: Shadowing
      5. Chapter Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Answers
      8. Test Your Knowledge: Part IV Exercises
  9. V. Modules and Packages
    1. 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
          1. Module sources
          2. Selection priorities
          3. Import hooks and ZIP files
          4. Optimized byte code files
      6. Chapter Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Answers
    2. 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
          1. Initialization code
        5. import and from Are Assignments
          1. Changing mutables in modules
          2. Cross-file name changes
        6. import and from Equivalence
        7. Potential Pitfalls of the from Statement
          1. When import is required
      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
    3. 24. Module Packages
      1. Package Import Basics
        1. Packages and Search Path Settings
        2. Package __init__.py Files
          1. Package initialization file roles
      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?
          1. The relative imports solution in 3.X
          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. Relative imports search packages only
          6. Imports are still relative to the CWD, again
        7. Pitfalls of Package-Relative Imports: Mixed Use
          1. The issue
          2. Fix 1: Package subdirectories
          3. Fix 2: Full path absolute import
          4. Example: Application to module self-test code (preview)
      5. Python 3.3 Namespace Packages
        1. Namespace Package Semantics
          1. The import algorithm
        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
    4. 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
          1. Testing recursive reloads
        2. Alternative Codings
          1. Testing reload variants
      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
  10. VI. Classes and OOP
    1. 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
          1. Polymorphism and classes
          2. Programming by customization
      3. Chapter Summary
      4. Test Your Knowledge: Quiz
      5. Test Your Knowledge: Answers
    2. 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
          1. Returning results, or not
        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
    3. 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
          1. The pickle module
          2. The shelve module
        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. 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
          1. Abstract superclasses in Python 3.X and 2.6+: Preview
      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
    5. 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
          1. Single versus multiple scans
          2. Classes versus generators
        2. Multiple Iterators on One Object
          1. Classes versus slices
        3. Coding Alternative: __iter__ plus yield
          1. Multiple iterators with 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
          1. Reusing __add__ in __radd__
          2. Propagating class type
        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
    6. 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
          1. Other callables
      7. Classes Are Objects: Generic Object Factories
        1. Why Factories?
      8. 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
          4. Running the tree lister
          5. Usage variation: Showing underscore name values
          6. Usage variation: Running on larger modules
          7. Collector module
          8. Room for improvement: MRO, slots, GUIs
      9. Other Design-Related Topics
      10. Chapter Summary
      11. Test Your Knowledge: Quiz
      12. Test Your Knowledge: Answers
    7. 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
          1. Why the lookup change?
          2. Implications for attribute interception
          3. Proxy coding requirements
          4. For more details
        2. Type Model Changes
          1. Implications for type testing
        3. All Classes Derive from “object”
          1. Implications for defaults
        4. Diamond Inheritance Change
          1. Implications for diamond inheritance trees
          2. Explicit conflict resolution
          3. Scope of search order change
        5. More on the MRO: Method Resolution Order
          1. The MRO algorithm
          2. Tracing the MRO
        6. Example: Mapping Attributes to Inheritance Sources
      4. New-Style Class Extensions
        1. Slots: Attribute Declarations
          1. Slot basics
          2. Slots and namespace dictionaries
          3. Multiple __slot__ lists in superclasses
          4. Handling slots and other “virtual” attributes generically
          5. Slot usage rules
          6. Example impacts of slots: ListTree and mapattrs
          7. What about slots speed?
        2. Properties: Attribute Accessors
          1. Property basics
        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
          1. Counting instances per class 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
          1. Odd semantics: A magic proxy in Python 3.X
          2. Pitfall: Adding multiple inheritance naively
          3. Limitation: Operator overloading
          4. Use differs in Python 2.X: Verbose calls
        4. The super Upsides: Tree Changes and Dispatch
        5. Runtime Class Changes and super
        6. Cooperative Multiple Inheritance Method Dispatch
          1. The basics: Cooperative super call in action
          2. Constraint: Call chain anchor requirement
          3. Scope: An all-or-nothing model
          4. Flexibility: Call ordering assumptions
          5. Customization: Method replacement
          6. Coupling: Application to mix-in classes
          7. Customization: Same-argument constraints
        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
          1. Choose per-instance or class storage wisely
          2. You usually want to call superclass constructors
          3. Delegation-based classes in 3.X: __getattr__ and built-ins
        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
  11. VII. Exceptions and Tools
    1. 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
    2. 34. Exception Coding Details
      1. The try/except/else Statement
        1. How try Statements Work
        2. try Statement Clauses
          1. Catching any and all exceptions
          2. Catching all: The empty except and Exception
        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
    3. 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
    4. 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
  12. VIII. Advanced Topics
    1. 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
          1. Why the different string types?
        4. Text and Binary Files
      3. Coding Basic Strings
        1. Python 3.X String Literals
          1. Python 2.X Unicode literals in Python 3.3
        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
          1. Mixing string types in 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
          1. Manual encoding
          2. File output encoding
          3. File input decoding
          4. Decoding mismatches
        2. Handling the BOM in 3.X
          1. Dropping the BOM in Notepad
          2. Dropping the BOM in Python
        3. Unicode Files in 2.X
        4. Unicode Filenames and Streams
          1. Filenames: Text versus bytes
          2. Stream content: PYTHONIOENCODING
      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
    2. 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
          1. Setter and deleter 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
          1. Descriptors and slots and more
      4. __getattr__ and __getattribute__
        1. The Basics
          1. Avoiding loops in attribute interception methods
        2. A First Example
          1. Using __getattribute__
        3. Computed Attributes
          1. Using __getattribute__
        4. __getattr__ and __getattribute__ Compared
        5. Management Techniques Compared
        6. Intercepting Built-in Operation Attributes
          1. Delegation-based managers revisited
      5. Example: Attribute Validations
        1. Using Properties to Validate
          1. Testing code
        2. Using Descriptors to Validate
          1. Option 1: Validating with shared descriptor instance state
          2. Option 2: Validating with per-client-instance state
        3. Using __getattr__ to Validate
        4. Using __getattribute__ to Validate
      6. Chapter Summary
      7. Test Your Knowledge: Quiz
        1. Test Your Knowledge: Answers
    3. 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
          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. Decorator State Retention Options
          1. Class instance attributes
          2. Enclosing scopes and globals
          3. Enclosing scopes and nonlocals
          4. Function attributes
        3. Class Blunders I: Decorating Methods
          1. Using nested functions to decorate methods
          2. Using descriptors to decorate methods
        4. Timing Calls
          1. Decorators versus per-call timing
          2. Testing subtleties
        5. Adding Decorator Arguments
          1. Timing with decorator arguments
      4. Coding Class Decorators
        1. Singleton Classes
          1. Coding alternatives
        2. Tracing Object Interfaces
          1. Tracing interfaces with class decorators
          2. Applying class decorators to built-in types
        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__ (and other virtual names)
        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: Implicitly run operator overloading methods fail to delegate under 3.X
          2. Approaches to redefining operator overloading methods for 3.X
            1. Inline definition
            2. Mix-in superclasses
            3. Coding variations: Routers, descriptors, automation
          3. Should operator methods be validated?
          4. 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
          1. Invalid calls
          2. Arbitrary arguments
          3. Decorator nesting
        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. 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
          1. Using simple factory functions
          2. Overloading class creation calls with normal classes
          3. Overloading class creation calls with metaclasses
      5. Inheritance and Instance
        1. Metaclass Versus Superclass
        2. Inheritance: The Full Story
          1. Python’s inheritance algorithm: The simple version
          2. The descriptors special case
          3. Python’s inheritance algorithm: The somewhat-more-complete version
          4. Assignment inheritance
          5. The built-ins special case
      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
          1. Decorator-based augmentation
          2. Managing instances instead of classes
          3. Metaclass and class decorator equivalence?
      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
    5. 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!
  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 (and older Windows)
          3. Windows environment variable GUI
          4. Windows registry
          5. Path files
        3. Python Command-Line Arguments
          1. Running script files with arguments
          2. Running code given in arguments and standard input
          3. Running modules on the search path
          4. Optimized and unbuffered modes
          5. Post-run interactive mode
          6. Python 2.X command-line arguments
        4. Python 3.3 Windows Launcher Command Lines
      3. For More Help
    2. 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
          1. Book examples impact and fix
        2. Pitfall 2: The Launcher Defaults to 2.X
          1. Book examples impact and fix
        3. Pitfall 3: The New PATH Extension Option
      6. Conclusions: A Net Win for Windows
    3. 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
          1. Standard library changes
          2. Tools changes
        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
    4. 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
  14. Index
  15. About the Author
  16. Colophon
  17. Copyright