Learning Python

B00DDZPC9S LP-OOP – Learning Python: Powerful Object-Oriented Programming 5th Edition, 2013, 1650 pages, by Mark Lutz 

Preface

This Book’s “Ecosystem”

About This Fifth Edition

The Python 2.X and 3.X LinesThe 2.X/3.X Story Today

Coverage for Both 3.X and 2.X

Which Python Should I Use?

This Book’s Prerequisites and Effort

This Book’s Structure

What This Book Is NotIt’s Not a Reference or a Guide to Specific Applications

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

It’s as Linear as Python Allows

This Book’s ProgramsPython Versions

Platforms

Fetching This Book’s Code

Using This Book’s Code

Font Conventions

Book Updates and Resources

AcknowledgmentsThe Backstory

Python Thanks

Personal Thanks

I. Getting Started

  1. A Python Q&A SessionWhy Do People Use Python?Software Quality

Developer Productivity

Is Python a “Scripting Language”?

OK, but What’s the Downside?

Who Uses Python Today?

What Can I Do with Python?

Systems Programming

GUIs with Python

Internet Scripting

Component Integration

Database Programming

Rapid Prototyping

Numeric and Scientific Programming

And More: Gaming, Images, Data Mining, Robots, Excel…

How Is Python Developed and Supported?Open Source Tradeoffs

  • Python’s Technical Strengths
    • It’s Object-Oriented and Functional
    • It’s Free
    • It’s Portable
    • It’s Powerful
    • It’s Mixable
    • It’s Relatively Easy to Use
    • It’s Relatively Easy to Learn
    • It’s Named After Monty Python

How Does Python Stack Up to Language X?

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. How Python Runs ProgramsIntroducing the Python Interpreter

Program ExecutionThe Programmer’s View

Python’s View

Execution Model VariationsPython Implementation Alternatives

Execution Optimization Tools

Frozen Binaries

Future Possibilities?

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. How You Run ProgramsThe Interactive PromptStarting an Interactive Session

The System Path

New Windows Options in 3.3: PATH, Launcher

Where to Run: Code Directories

What Not to Type: Prompts and Comments

Running Code Interactively

Why the Interactive Prompt?

Usage Notes: The Interactive Prompt

System Command Lines and FilesA First Script

Running Files with Command Lines

Command-Line Usage Variations

Usage Notes: Command Lines and Files

Unix-Style Executable Scripts: #!Unix Script Basics

The Unix env Lookup Trick

The Python 3.3 Windows Launcher: #! Comes to Windows

Clicking File IconsIcon-Click Basics

Clicking Icons on Windows

The input Trick on Windows

Other Icon-Click Limitations

Module Imports and ReloadsImport and Reload Basics

The Grander Module Story: Attributes

Usage Notes: import and reload

Using exec to Run Module Files

The IDLE User InterfaceIDLE Startup Details

IDLE Basic Usage

IDLE Usability Features

Advanced IDLE Tools

Usage Notes: IDLE

Other IDEs

Other Launch OptionsEmbedding Calls

Frozen Binary Executables

Text Editor Launch Options

Still Other Launch Options

Future Possibilities?

Which Option Should I Use?

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part I Exercises

II. Types and Operations

  1. Introducing Python Object TypesThe Python Conceptual Hierarchy

Why Use Built-in Types?

Python’s Core Data Types

Numbers

Strings

Sequence Operations

Immutability

Type-Specific Methods

Getting Help

Other Ways to Code Strings

Unicode Strings

Pattern Matching

ListsSequence Operations

Type-Specific Operations

Bounds Checking

Nesting

Comprehensions

DictionariesMapping Operations

Nesting Revisited

Missing Keys: if Tests

Sorting Keys: for Loops

Iteration and Optimization

Tuples

Why Tuples?

FilesBinary Bytes Files

Unicode Text Files

Other File-Like Tools

Other Core TypesHow to Break Your Code’s Flexibility

User-Defined Classes

And Everything Else

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Numeric TypesNumeric Type BasicsNumeric Literals

Built-in Numeric Tools

Python Expression Operators

Numbers in Action

Variables and Basic Expressions

Numeric Display Formats

Comparisons: Normal and Chained

Division: Classic, Floor, and True

Integer Precision

Complex Numbers

Hex, Octal, Binary: Literals and Conversions

Bitwise Operations

Other Built-in Numeric Tools

Other Numeric TypesDecimal Type

Fraction Type

Sets

Booleans

Numeric Extensions

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. The Dynamic Typing InterludeThe Case of the Missing Declaration StatementsVariables, Objects, and References

Types Live with Objects, Not Variables

Objects Are Garbage-Collected

Shared ReferencesShared References and In-Place Changes

Shared References and Equality

Dynamic Typing Is Everywhere

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. String FundamentalsThis Chapter’s ScopeUnicode: The Short Story

String Basics

String LiteralsSingle- and Double-Quoted Strings Are the Same

Escape Sequences Represent Special Characters

Raw Strings Suppress Escapes

Triple Quotes Code Multiline Block Strings

Strings in ActionBasic Operations

Indexing and Slicing

String Conversion Tools

Changing Strings I

String MethodsMethod Call Syntax

Methods of Strings

String Method Examples: Changing Strings II

String Method Examples: Parsing Text

Other Common String Methods in Action

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

String Formatting ExpressionsFormatting Expression Basics

Advanced Formatting Expression Syntax

Advanced Formatting Expression Examples

Dictionary-Based Formatting Expressions

String Formatting Method CallsFormatting Method Basics

Adding Keys, Attributes, and Offsets

Advanced Formatting Method Syntax

Advanced Formatting Method Examples

Comparison to the % Formatting Expression

Why the Format Method?

General Type CategoriesTypes Share Operation Sets by Categories

Mutable Types Can Be Changed in Place

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Lists and DictionariesLists

Lists in ActionBasic List Operations

List Iteration and Comprehensions

Indexing, Slicing, and Matrixes

Changing Lists in Place

Dictionaries

Dictionaries in ActionBasic Dictionary Operations

Changing Dictionaries in Place

More Dictionary Methods

Example: Movie Database

Dictionary Usage Notes

Other Ways to Make Dictionaries

Dictionary Changes in Python 3.X and 2.7

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Tuples, Files, and Everything ElseTuplesTuples in Action

Why Lists and Tuples?

Records Revisited: Named Tuples

FilesOpening Files

Using Files

Files in Action

Text and Binary Files: The Short Story

Storing Python Objects in Files: Conversions

Storing Native Python Objects: pickle

Storing Python Objects in JSON Format

Storing Packed Binary Data: struct

File Context Managers

Other File Tools

Core Types Review and SummaryObject Flexibility

References Versus Copies

Comparisons, Equality, and Truth

The Meaning of True and False in Python

Python’s Type Hierarchies

Type Objects

Other Types in Python

Built-in Type GotchasAssignment Creates References, Not Copies

Repetition Adds One Level Deep

Beware of Cyclic Data Structures

Immutable Types Can’t Be Changed in Place

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part II Exercises

III. Statements and Syntax

  1. Introducing Python StatementsThe Python Conceptual Hierarchy Revisited

Python’s Statements

A Tale of Two ifsWhat Python Adds

What Python Removes

Why Indentation Syntax?

A Few Special Cases

A Quick Example: Interactive LoopsA Simple Interactive Loop

Doing Math on User Inputs

Handling Errors by Testing Inputs

Handling Errors with try Statements

Nesting Code Three Levels Deep

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Assignments, Expressions, and PrintsAssignment StatementsAssignment Statement Forms

Sequence Assignments

Extended Sequence Unpacking in Python 3.X

Multiple-Target Assignments

Augmented Assignments

Variable Name Rules

Expression StatementsExpression Statements and In-Place Changes

Print OperationsThe Python 3.X print Function

The Python 2.X print Statement

Print Stream Redirection

Version-Neutral Printing

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. if Tests and Syntax Rulesif StatementsGeneral Format

Basic Examples

Multiway Branching

Python Syntax RevisitedBlock Delimiters: Indentation Rules

Statement Delimiters: Lines and Continuations

A Few Special Cases

Truth Values and Boolean Tests

The if/else Ternary Expression

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. while and for Loopswhile LoopsGeneral Format

Examples

break, continue, pass, and the Loop elseGeneral Loop Format

pass

continue

break

Loop else

for LoopsGeneral Format

Examples

Loop Coding TechniquesCounter Loops: range

Sequence Scans: while and range Versus for

Sequence Shufflers: range and len

Nonexhaustive Traversals: range Versus Slices

Changing Lists: range Versus Comprehensions

Parallel Traversals: zip and map

Generating Both Offsets and Items: enumerate

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Iterations and ComprehensionsIterations: A First LookThe Iteration Protocol: File Iterators

Manual Iteration: iter and next

Other Built-in Type Iterables

List Comprehensions: A First Detailed LookList Comprehension Basics

Using List Comprehensions on Files

Extended List Comprehension Syntax

Other Iteration Contexts

New Iterables in Python 3.XImpacts on 2.X Code: Pros and Cons

The range Iterable

The map, zip, and filter Iterables

Multiple Versus Single Pass Iterators

Dictionary View Iterables

Other Iteration Topics

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. The Documentation InterludePython Documentation Sources# Comments

The dir Function

Docstrings: doc

PyDoc: The help Function

PyDoc: HTML Reports

Beyond docstrings: Sphinx

The Standard Manual Set

Web Resources

Published Books

Common Coding Gotchas

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part III Exercises

IV. Functions and Generators

  1. Function BasicsWhy Use Functions?

Coding Functionsdef Statements

def Executes at Runtime

A First Example: Definitions and CallsDefinition

Calls

Polymorphism in Python

A Second Example: Intersecting SequencesDefinition

Calls

Polymorphism Revisited

Local Variables

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. ScopesPython Scope BasicsScope Details

Name Resolution: The LEGB Rule

Scope Example

The Built-in Scope

The global StatementProgram Design: Minimize Global Variables

Program Design: Minimize Cross-File Changes

Other Ways to Access Globals

Scopes and Nested FunctionsNested Scope Details

Nested Scope Examples

Factory Functions: Closures

Retaining Enclosing Scope State with Defaults

The nonlocal Statement in 3.Xnonlocal Basics

nonlocal in Action

Why nonlocal? State Retention OptionsState with nonlocal: 3.X only

State with Globals: A Single Copy Only

State with Classes: Explicit Attributes (Preview)

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

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. ArgumentsArgument-Passing BasicsArguments and Shared References

Avoiding Mutable Argument Changes

Simulating Output Parameters and Multiple Results

Special Argument-Matching ModesArgument Matching Basics

Argument Matching Syntax

The Gritty Details

Keyword and Default Examples

Arbitrary Arguments Examples

Python 3.X Keyword-Only Arguments

The min Wakeup Call!Full Credit

Bonus Points

The Punch Line…

Generalized Set Functions

Emulating the Python 3.X print FunctionUsing Keyword-Only Arguments

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Advanced Function TopicsFunction Design Concepts

Recursive FunctionsSummation with Recursion

Coding Alternatives

Loop Statements Versus Recursion

Handling Arbitrary Structures

Function Objects: Attributes and AnnotationsIndirect Function Calls: “First Class” Objects

Function Introspection

Function Attributes

Function Annotations in 3.X

Anonymous Functions: lambdalambda Basics

Why Use lambda?

How (Not) to Obfuscate Your Python Code

Scopes: lambdas Can Be Nested Too

Functional Programming ToolsMapping Functions over Iterables: map

Selecting Items in Iterables: filter

Combining Items in Iterables: reduce

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Comprehensions and GenerationsList Comprehensions and Functional ToolsList Comprehensions Versus map

Adding Tests and Nested Loops: filter

Example: List Comprehensions and Matrixes

Don’t Abuse List Comprehensions: KISS

Generator Functions and ExpressionsGenerator Functions: yield Versus return

Generator Expressions: Iterables Meet Comprehensions

Generator Functions Versus Generator Expressions

Generators Are Single-Iteration Objects

Generation in Built-in Types, Tools, and Classes

Example: Generating Scrambled Sequences

Don’t Abuse Generators: EIBTI

Example: Emulating zip and map with Iteration Tools

Comprehension Syntax SummaryScopes and Comprehension Variables

Comprehending Set and Dictionary Comprehensions

Extended Comprehension Syntax for Sets and Dictionaries

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. The Benchmarking InterludeTiming Iteration AlternativesTiming Module: Homegrown

Timing Script

Timing Results

Timing Module Alternatives

Other Suggestions

Timing Iterations and Pythons with timeitBasic timeit Usage

Benchmark Module and Script: timeit

Benchmark Script Results

More Fun with Benchmarks

Other Benchmarking Topics: pystones

Function GotchasLocal Names Are Detected Statically

Defaults and Mutable Objects

Functions Without returns

Miscellaneous Function Gotchas

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part IV Exercises

V. Modules and Packages

  1. Modules: The Big PictureWhy Use Modules?

Python Program ArchitectureHow to Structure a Program

Imports and Attributes

Standard Library Modules

How Imports Work1. Find It

  1. Compile It (Maybe)
  2. Run It

Byte Code Files: pycache in Python 3.2+Byte Code File Models in Action

The Module Search PathConfiguring the Search Path

Search Path Variations

The sys.path List

Module File Selection

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Module Coding BasicsModule CreationModule Filenames

Other Kinds of Modules

Module UsageThe import Statement

The from Statement

The from * Statement

Imports Happen Only Once

import and from Are Assignments

import and from Equivalence

Potential Pitfalls of the from Statement

Module NamespacesFiles Generate Namespaces

Namespace Dictionaries: dict

Attribute Name Qualification

Imports Versus Scopes

Namespace Nesting

Reloading Modulesreload Basics

reload Example

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Module PackagesPackage Import BasicsPackages and Search Path Settings

Package init.py Files

Package Import Examplefrom Versus import with Packages

Why Use Package Imports?A Tale of Three Systems

Package Relative ImportsChanges in Python 3.X

Relative Import Basics

Why Relative Imports?

The Scope of Relative Imports

Module Lookup Rules Summary

Relative Imports in Action

Pitfalls of Package-Relative Imports: Mixed Use

Python 3.3 Namespace PackagesNamespace Package Semantics

Impacts on Regular Packages: Optional init.py

Namespace Packages in Action

Namespace Package Nesting

Files Still Have Precedence over Directories

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Advanced Module TopicsModule Design Concepts

Data Hiding in ModulesMinimizing from * Damage: _X and all

Enabling Future Language Features: future

Mixed Usage Modes: name and main__Unit Tests with __name

Example: Dual Mode CodeCurrency Symbols: Unicode in Action

Docstrings: Module Documentation at Work

Changing the Module Search Path

The as Extension for import and from

Example: Modules Are Objects

Importing Modules by Name StringRunning Code Strings

Direct Calls: Two Options

Example: Transitive Module ReloadsA Recursive Reloader

Alternative Codings

Module GotchasModule Name Clashes: Package and Package-Relative Imports

Statement Order Matters in Top-Level Code

from Copies Names but Doesn’t Link

from * Can Obscure the Meaning of Variables

reload May Not Impact from Imports

reload, from, and Interactive Testing

Recursive from Imports May Not Work

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part V Exercises

VI. Classes and OOP

  1. OOP: The Big PictureWhy Use Classes?

OOP from 30,000 FeetAttribute Inheritance Search

Classes and Instances

Method Calls

Coding Class Trees

Operator Overloading

OOP Is About Code Reuse

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Class Coding BasicsClasses Generate Multiple Instance ObjectsClass Objects Provide Default Behavior

Instance Objects Are Concrete Items

A First Example

Classes Are Customized by InheritanceA Second Example

Classes Are Attributes in Modules

Classes Can Intercept Python OperatorsA Third Example

Why Use Operator Overloading?

The World’s Simplest Python ClassRecords Revisited: Classes Versus Dictionaries

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. A More Realistic ExampleStep 1: Making InstancesCoding Constructors

Testing As You Go

Using Code Two Ways

Step 2: Adding Behavior MethodsCoding Methods

Step 3: Operator OverloadingProviding Print Displays

Step 4: Customizing Behavior by SubclassingCoding Subclasses

Augmenting Methods: The Bad Way

Augmenting Methods: The Good Way

Polymorphism in Action

Inherit, Customize, and Extend

OOP: The Big Idea

Step 5: Customizing Constructors, TooOOP Is Simpler Than You May Think

Other Ways to Combine Classes

Step 6: Using Introspection ToolsSpecial Class Attributes

A Generic Display Tool

Instance Versus Class Attributes

Name Considerations in Tool Classes

Our Classes’ Final Form

Step 7 (Final): Storing Objects in a DatabasePickles and Shelves

Storing Objects on a Shelve Database

Exploring Shelves Interactively

Updating Objects on a Shelve

Future Directions

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Class Coding DetailsThe class StatementGeneral Form

Example

MethodsMethod Example

Calling Superclass Constructors

Other Method Call Possibilities

InheritanceAttribute Tree Construction

Specializing Inherited Methods

Class Interface Techniques

Abstract Superclasses

Namespaces: The ConclusionSimple Names: Global Unless Assigned

Attribute Names: Object Namespaces

The “Zen” of Namespaces: Assignments Classify Names

Nested Classes: The LEGB Scopes Rule Revisited

Namespace Dictionaries: Review

Namespace Links: A Tree Climber

Documentation Strings Revisited

Classes Versus Modules

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Operator OverloadingThe BasicsConstructors and Expressions: init and sub

Common Operator Overloading Methods

Indexing and Slicing: getitem and __setitem__Intercepting Slices

Slicing and Indexing in Python 2.X

But 3.X’s index Is Not Indexing!

Index Iteration: getitem

Iterable Objects: iter and __next__User-Defined Iterables

Multiple Iterators on One Object

Coding Alternative: iter plus yield

Membership: contains, iter, and getitem

Attribute Access: getattr and __setattr__Attribute Reference

Attribute Assignment and Deletion

Other Attribute Management Tools

Emulating Privacy for Instance Attributes: Part 1

String Representation: repr and __str__Why Two Display Methods?

Display Usage Notes

Right-Side and In-Place Uses: radd and __iadd__Right-Side Addition

In-Place Addition

Call Expressions: __call__Function Interfaces and Callback-Based Code

Comparisons: lt, gt, and OthersThe cmp Method in Python 2.X

Boolean Tests: bool and __len__Boolean Methods in Python 2.X

Object Destruction: __del__Destructor Usage Notes

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Designing with ClassesPython and OOPPolymorphism Means Interfaces, Not Call Signatures

OOP and Inheritance: “Is-a” Relationships

OOP and Composition: “Has-a” RelationshipsStream Processors Revisited

OOP and Delegation: “Wrapper” Proxy Objects

Pseudoprivate Class AttributesName Mangling Overview

Why Use Pseudoprivate Attributes?

Methods Are Objects: Bound or UnboundUnbound Methods Are Functions in 3.X

Bound Methods and Other Callable Objects

Classes Are Objects: Generic Object FactoriesWhy Factories?

Multiple Inheritance: “Mix-in” ClassesCoding Mix-in Display Classes

Other Design-Related Topics

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Advanced Class TopicsExtending Built-in TypesExtending Types by Embedding

Extending Types by Subclassing

The “New Style” Class ModelJust How New Is New-Style?

New-Style Class ChangesAttribute Fetch for Built-ins Skips Instances

Type Model Changes

All Classes Derive from “object”

Diamond Inheritance Change

More on the MRO: Method Resolution Order

Example: Mapping Attributes to Inheritance Sources

New-Style Class ExtensionsSlots: Attribute Declarations

Properties: Attribute Accessors

getattribute and Descriptors: Attribute Tools

Other Class Changes and Extensions

Static and Class MethodsWhy the Special Methods?

Static Methods in 2.X and 3.X

Static Method Alternatives

Using Static and Class Methods

Counting Instances with Static Methods

Counting Instances with Class Methods

Decorators and Metaclasses: Part 1Function Decorator Basics

A First Look at User-Defined Function Decorators

A First Look at Class Decorators and Metaclasses

For More Details

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

Traditional Superclass Call Form: Portable, General

Basic super Usage and Its Tradeoffs

The super Upsides: Tree Changes and Dispatch

Runtime Class Changes and super

Cooperative Multiple Inheritance Method Dispatch

The super Summary

Class GotchasChanging Class Attributes Can Have Side Effects

Changing Mutable Class Attributes Can Have Side Effects, Too

Multiple Inheritance: Order Matters

Scopes in Methods and Classes

Miscellaneous Class Gotchas

KISS Revisited: “Overwrapping-itis”

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part VI Exercises

VII. Exceptions and Tools

  1. Exception BasicsWhy Use Exceptions?Exception Roles

Exceptions: The Short StoryDefault Exception Handler

Catching Exceptions

Raising Exceptions

User-Defined Exceptions

Termination Actions

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Exception Coding DetailsThe try/except/else StatementHow try Statements Work

try Statement Clauses

The try else Clause

Example: Default Behavior

Example: Catching Built-in Exceptions

The try/finally StatementExample: Coding Termination Actions with try/finally

Unified try/except/finallyUnified try Statement Syntax

Combining finally and except by Nesting

Unified try Example

The raise StatementRaising Exceptions

Scopes and try except Variables

Propagating Exceptions with raise

Python 3.X Exception Chaining: raise from

The assert StatementExample: Trapping Constraints (but Not Errors!)

with/as Context ManagersBasic Usage

The Context Management Protocol

Multiple Context Managers in 3.1, 2.7, and Later

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Exception ObjectsExceptions: Back to the FutureString Exceptions Are Right Out!

Class-Based Exceptions

Coding Exceptions Classes

Why Exception Hierarchies?

Built-in Exception ClassesBuilt-in Exception Categories

Default Printing and State

Custom Print Displays

Custom Data and BehaviorProviding Exception Details

Providing Exception Methods

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Designing with ExceptionsNesting Exception HandlersExample: Control-Flow Nesting

Example: Syntactic Nesting

Exception IdiomsBreaking Out of Multiple Nested Loops: “go to”

Exceptions Aren’t Always Errors

Functions Can Signal Conditions with raise

Closing Files and Server Connections

Debugging with Outer try Statements

Running In-Process Tests

More on sys.exc_info

Displaying Errors and Tracebacks

Exception Design Tips and GotchasWhat Should Be Wrapped

Catching Too Much: Avoid Empty except and Exception

Catching Too Little: Use Class-Based Categories

Core Language SummaryThe Python Toolset

Development Tools for Larger Projects

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part VII Exercises

VIII. Advanced Topics

  1. Unicode and Byte StringsString Changes in 3.X

String BasicsCharacter Encoding Schemes

How Python Stores Strings in Memory

Python’s String Types

Text and Binary Files

Coding Basic StringsPython 3.X String Literals

Python 2.X String Literals

String Type Conversions

Coding Unicode StringsCoding ASCII Text

Coding Non-ASCII Text

Encoding and Decoding Non-ASCII text

Other Encoding Schemes

Byte String Literals: Encoded Text

Converting Encodings

Coding Unicode Strings in Python 2.X

Source File Character Set Encoding Declarations

Using 3.X bytes ObjectsMethod Calls

Sequence Operations

Other Ways to Make bytes Objects

Mixing String Types

Using 3.X/2.6+ bytearray Objectsbytearrays in Action

Python 3.X String Types Summary

Using Text and Binary FilesText File Basics

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

Type and Content Mismatches in 3.X

Using Unicode FilesReading and Writing Unicode in 3.X

Handling the BOM in 3.X

Unicode Files in 2.X

Unicode Filenames and Streams

Other String Tool Changes in 3.XThe re Pattern-Matching Module

The struct Binary Data Module

The pickle Object Serialization Module

XML Parsing Tools

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. Managed AttributesWhy Manage Attributes?Inserting Code to Run on Attribute Access

PropertiesThe Basics

A First Example

Computed Attributes

Coding Properties with Decorators

DescriptorsThe Basics

A First Example

Computed Attributes

Using State Information in Descriptors

How Properties and Descriptors Relate

getattr and __getattribute__The Basics

A First Example

Computed Attributes

getattr and getattribute Compared

Management Techniques Compared

Intercepting Built-in Operation Attributes

Example: Attribute ValidationsUsing Properties to Validate

Using Descriptors to Validate

Using getattr to Validate

Using getattribute to Validate

Chapter Summary

Test Your Knowledge: QuizTest Your Knowledge: Answers

  1. DecoratorsWhat’s a Decorator?Managing Calls and Instances

Managing Functions and Classes

Using and Defining Decorators

Why Decorators?

The BasicsFunction Decorators

Class Decorators

Decorator Nesting

Decorator Arguments

Decorators Manage Functions and Classes, Too

Coding Function DecoratorsTracing Calls

Decorator State Retention Options

Class Blunders I: Decorating Methods

Timing Calls

Adding Decorator Arguments

Coding Class DecoratorsSingleton Classes

Tracing Object Interfaces

Class Blunders II: Retaining Multiple Instances

Decorators Versus Manager Functions

Why Decorators? (Revisited)

Managing Functions and Classes Directly

Example: “Private” and “Public” AttributesImplementing Private Attributes

Implementation Details I

Generalizing for Public Declarations, Too

Implementation Details II

Open Issues

Python Isn’t About Control

Example: Validating Function ArgumentsThe Goal

A Basic Range-Testing Decorator for Positional Arguments

Generalizing for Keywords and Defaults, Too

Implementation Details

Open Issues

Decorator Arguments Versus Function Annotations

Other Applications: Type Testing (If You Insist!)

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. MetaclassesTo Metaclass or Not to MetaclassIncreasing Levels of “Magic”

A Language of Hooks

The Downside of “Helper” Functions

Metaclasses Versus Class Decorators: Round 1

The Metaclass ModelClasses Are Instances of type

Metaclasses Are Subclasses of Type

Class Statement Protocol

Declaring MetaclassesDeclaration in 3.X

Declaration in 2.X

Metaclass Dispatch in Both 3.X and 2.X

Coding MetaclassesA Basic Metaclass

Customizing Construction and Initialization

Other Metaclass Coding Techniques

Inheritance and InstanceMetaclass Versus Superclass

Inheritance: The Full Story

Metaclass MethodsMetaclass Methods Versus Class Methods

Operator Overloading in Metaclass Methods

Example: Adding Methods to ClassesManual Augmentation

Metaclass-Based Augmentation

Metaclasses Versus Class Decorators: Round 2

Example: Applying Decorators to MethodsTracing with Decoration Manually

Tracing with Metaclasses and Decorators

Applying Any Decorator to Methods

Metaclasses Versus Class Decorators: Round 3 (and Last)

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

  1. All Good ThingsThe Python ParadoxOn “Optional” Language Features

Against Disquieting Improvements

Complexity Versus Power

Simplicity Versus Elitism

Closing Thoughts

Where to Go From Here

Encore: Print Your Own Completion Certificate!

IX. Appendixes

A. Installation and ConfigurationInstalling the Python InterpreterIs Python Already Present?

Where to Get Python

Installation Steps

Configuring PythonPython Environment Variables

How to Set Configuration Options

Python Command-Line Arguments

Python 3.3 Windows Launcher Command Lines

For More Help

B. The Python 3.3 Windows LauncherThe Unix Legacy

The Windows Legacy

Introducing the New Windows Launcher

A Windows Launcher TutorialStep 1: Using Version Directives in Files

Step 2: Using Command-Line Version Switches

Step 3: Using and Changing Defaults

Pitfalls of the New Windows LauncherPitfall 1: Unrecognized Unix #! Lines Fail

Pitfall 2: The Launcher Defaults to 2.X

Pitfall 3: The New PATH Extension Option

Conclusions: A Net Win for Windows

C. Python Changes and This BookMajor 2.X/3.X Differences3.X Differences

3.X-Only Extensions

General Remarks: 3.X ChangesChanges in Libraries and Tools

Migrating to 3.X

Fifth Edition Python Changes: 2.7, 3.2, 3.3Changes in Python 2.7

Changes in Python 3.3

Changes in Python 3.2

Fourth Edition Python Changes: 2.6, 3.0, 3.1Changes in Python 3.1

Changes in Python 3.0 and 2.6

Specific Language Removals in 3.0

Third Edition Python Changes: 2.3, 2.4, 2.5

Earlier and Later Python Changes

D. Solutions to End-of-Part ExercisesPart I, Getting Started

Part II, Types and Operations

Part III, Statements and Syntax

Part IV, Functions and Generators

Part V, Modules and Packages

Part VI, Classes and OOP

Part VII, Exceptions and Tools