Home Page Icon
Home Page
Table of Contents for
VIII. Advanced Topics
Close
VIII. Advanced Topics
by Mark Lutz
Learning Python, 4th Edition
Dedication
A Note Regarding Supplemental Files
Preface
About This Fourth Edition
Coverage for Both 3.0 and 2.6
New Chapters
Changes to Existing Material
Specific Language Extensions in 2.6 and 3.0
Specific Language Removals in 3.0
About The Third Edition
The Third Edition’s Python Language Changes
The Third Edition’s Python Training Changes
The Third Edition’s Structural Changes
The Third Edition’s Scope Changes
About This Book
This Book’s Prerequisites
This Book’s Scope and Other Books
This Book’s Style and Structure
Book Updates
About the Programs in This Book
Using Code Examples
Font Conventions
Safari® Books Online
How to Contact Us
Acknowledgments
I. Getting Started
1. A Python Q&A Session
Why Do People Use Python?
Software Quality
Developer Productivity
Is Python a “Scripting Language”?
OK, but What’s the Downside?
Who Uses Python Today?
What Can I Do with Python?
Systems Programming
GUIs
Internet Scripting
Component Integration
Database Programming
Rapid Prototyping
Numeric and Scientific Programming
Gaming, Images, Serial Ports, XML, Robots, and More
How Is Python Supported?
What Are Python’s Technical Strengths?
It’s Object-Oriented
It’s Free
It’s Portable
It’s Powerful
It’s Mixable
It’s Easy to Use
It’s Easy to Learn
It’s Named After Monty Python
How Does Python Stack Up to Language X?
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
2. How Python Runs Programs
Introducing the Python Interpreter
Program Execution
The Programmer’s View
Python’s View
Byte code compilation
The Python Virtual Machine (PVM)
Performance implications
Development implications
Execution Model Variations
Python Implementation Alternatives
CPython
Jython
IronPython
Execution Optimization Tools
The Psyco just-in-time compiler
The Shedskin C++ translator
Frozen Binaries
Other Execution Options
Future Possibilities?
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
3. How You Run Programs
The Interactive Prompt
Running Code Interactively
Why the Interactive Prompt?
Experimenting
Testing
Using the Interactive Prompt
Entering multiline statements
System Command Lines and Files
A First Script
Running Files with Command Lines
Using Command Lines and Files
Unix Executable Scripts (#!)
Clicking File Icons
Clicking Icons on Windows
The input Trick
Other Icon-Click Limitations
Module Imports and Reloads
The Grander Module Story: Attributes
Modules and namespaces
import and reload Usage Notes
Using exec to Run Module Files
The IDLE User Interface
IDLE Basics
Using IDLE
Advanced IDLE Tools
Other IDEs
Other Launch Options
Embedding Calls
Frozen Binary Executables
Text Editor Launch Options
Still Other Launch Options
Future Possibilities?
Which Option Should I Use?
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part I Exercises
II. Types and Operations
4. Introducing Python Object Types
Why Use Built-in Types?
Python’s Core Data Types
Numbers
Strings
Sequence Operations
Immutability
Type-Specific Methods
Getting Help
Other Ways to Code Strings
Pattern Matching
Lists
Sequence Operations
Type-Specific Operations
Bounds Checking
Nesting
Comprehensions
Dictionaries
Mapping Operations
Nesting Revisited
Sorting Keys: for Loops
Iteration and Optimization
Missing Keys: if Tests
Tuples
Why Tuples?
Files
Other File-Like Tools
Other Core Types
How to Break Your Code’s Flexibility
User-Defined Classes
And Everything Else
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
5. Numeric Types
Numeric Type Basics
Numeric Literals
Built-in Numeric Tools
Python Expression Operators
Mixed operators follow operator precedence
Parentheses group subexpressions
Mixed types are converted up
Preview: Operator overloading and polymorphism
Numbers in Action
Variables and Basic Expressions
Numeric Display Formats
Comparisons: Normal and Chained
Division: Classic, Floor, and True
Supporting either Python
Floor versus truncation
Why does truncation matter?
Integer Precision
Complex Numbers
Hexadecimal, Octal, and Binary Notation
Bitwise Operations
Other Built-in Numeric Tools
Other Numeric Types
Decimal Type
The basics
Setting precision globally
Decimal context manager
Fraction Type
The basics
Numeric accuracy
Conversions and mixed types
Sets
Set basics in Python 2.6
Set literals in Python 3.0
Immutable constraints and frozen sets
Set comprehensions in Python 3.0
Why sets?
Booleans
Numeric Extensions
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
6. The Dynamic Typing Interlude
The Case of the Missing Declaration Statements
Variables, Objects, and References
Types Live with Objects, Not Variables
Objects Are Garbage-Collected
Shared References
Shared References and In-Place Changes
Shared References and Equality
Dynamic Typing Is Everywhere
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
7. Strings
String Literals
Single- and Double-Quoted Strings Are the Same
Escape Sequences Represent Special Bytes
Raw Strings Suppress Escapes
Triple Quotes Code Multiline Block Strings
Strings in Action
Basic Operations
Indexing and Slicing
Extended slicing: the third limit and slice objects
String Conversion Tools
Character code conversions
Changing Strings
String Methods
String Method Examples: Changing Strings
String Method Examples: Parsing Text
Other Common String Methods in Action
The Original string Module (Gone in 3.0)
String Formatting Expressions
Advanced String Formatting Expressions
Dictionary-Based String Formatting Expressions
String Formatting Method Calls
The Basics
Adding Keys, Attributes, and Offsets
Adding Specific Formatting
Comparison to the % Formatting Expression
Why the New Format Method?
Extra features
Explicit value references
Method names and general arguments
Possible future deprecation?
General Type Categories
Types Share Operation Sets by Categories
Mutable Types Can Be Changed In-Place
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
8. Lists and Dictionaries
Lists
Lists in Action
Basic List Operations
List Iteration and Comprehensions
Indexing, Slicing, and Matrixes
Changing Lists In-Place
Index and slice assignments
List method calls
Other common list operations
Dictionaries
Dictionaries in Action
Basic Dictionary Operations
Changing Dictionaries In-Place
More Dictionary Methods
A Languages Table
Dictionary Usage Notes
Using dictionaries to simulate flexible lists
Using dictionaries for sparse data structures
Avoiding missing-key errors
Using dictionaries as “records”
Other Ways to Make Dictionaries
Dictionary Changes in Python 3.0
Dictionary comprehensions
Dictionary views
Dictionary views and sets
Sorting dictionary keys
Dictionary magnitude comparisons no longer work
The has_key method is dead: long live in!
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
9. Tuples, Files, and Everything Else
Tuples
Tuples in Action
Tuple syntax peculiarities: Commas and parentheses
Conversions, methods, and immutability
Why Lists and Tuples?
Files
Opening Files
Using Files
Files in Action
Text and binary files in Python 3.0
Storing and parsing Python objects in files
Storing native Python objects with pickle
Storing and parsing packed binary data in files
File context managers
Other File Tools
Type Categories Revisited
Object Flexibility
References Versus Copies
Comparisons, Equality, and Truth
Python 3.0 Dictionary Comparisons
The Meaning of True and False in Python
The None object
The bool type
Python’s Type Hierarchies
Type Objects
Other Types in Python
Built-in Type Gotchas
Assignment Creates References, Not Copies
Repetition Adds One Level Deep
Beware of Cyclic Data Structures
Immutable Types Can’t Be Changed In-Place
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part II Exercises
III. Statements and Syntax
10. Introducing Python Statements
Python Program Structure Revisited
Python’s Statements
A Tale of Two ifs
What Python Adds
What Python Removes
Parentheses are optional
End of line is end of statement
End of indentation is end of block
Why Indentation Syntax?
A Few Special Cases
Statement rule special cases
Block rule special case
A Quick Example: Interactive Loops
A Simple Interactive Loop
Doing Math on User Inputs
Handling Errors by Testing Inputs
Handling Errors with try Statements
Nesting Code Three Levels Deep
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
11. Assignments, Expressions, and Prints
Assignment Statements
Assignment Statement Forms
Sequence Assignments
Advanced sequence assignment patterns
Extended Sequence Unpacking in Python 3.0
Extended unpacking in action
Boundary cases
A useful convenience
Application to for loops
Multiple-Target Assignments
Multiple-target assignment and shared references
Augmented Assignments
Augmented assignment and shared references
Variable Name Rules
Naming conventions
Names have no type, but objects do
Expression Statements
Expression Statements and In-Place Changes
Print Operations
The Python 3.0 print Function
Call format
The 3.0 print function in action
The Python 2.6 print Statement
Statement forms
The 2.6 print statement in action
Print Stream Redirection
The Python “hello world” program
Manual stream redirection
Automatic stream redirection
Version-Neutral Printing
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
12. if Tests and Syntax Rules
if Statements
General Format
Basic Examples
Multiway Branching
Python Syntax Rules
Block Delimiters: Indentation Rules
Avoid mixing tabs and spaces: New error checking in 3.0
Statement Delimiters: Lines and Continuations
A Few Special Cases
Truth Tests
The if/else Ternary Expression
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
13. while and for Loops
while Loops
General Format
Examples
break, continue, pass, and the Loop else
General Loop Format
pass
continue
break
Loop else
More on the loop else
for Loops
General Format
Examples
Basic usage
Other data types
Tuple assignment in for loops
Python 3.0 extended sequence assignment in for loops
Nested for loops
Loop Coding Techniques
Counter Loops: while and range
Nonexhaustive Traversals: range and Slices
Changing Lists: range
Parallel Traversals: zip and map
map equivalence in Python 2.6
Dictionary construction with zip
Generating Both Offsets and Items: enumerate
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
14. Iterations and Comprehensions, Part 1
Iterators: A First Look
The Iteration Protocol: File Iterators
Manual Iteration: iter and next
Other Built-in Type Iterators
List Comprehensions: A First Look
List Comprehension Basics
Using List Comprehensions on Files
Extended List Comprehension Syntax
Other Iteration Contexts
New Iterables in Python 3.0
The range Iterator
The map, zip, and filter Iterators
Multiple Versus Single Iterators
Dictionary View Iterators
Other Iterator Topics
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
15. The Documentation Interlude
Python Documentation Sources
# Comments
The dir Function
Docstrings: __doc__
User-defined docstrings
Docstring standards
Built-in docstrings
PyDoc: The help Function
PyDoc: HTML Reports
The Standard Manual Set
Web Resources
Published Books
Common Coding Gotchas
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part III Exercises
IV. Functions
16. Function Basics
Why Use Functions?
Coding Functions
def Statements
def Executes at Runtime
A First Example: Definitions and Calls
Definition
Calls
Polymorphism in Python
A Second Example: Intersecting Sequences
Definition
Calls
Polymorphism Revisited
Local Variables
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
17. Scopes
Python Scope Basics
Scope Rules
Name Resolution: The LEGB Rule
Scope Example
The Built-in Scope
The global Statement
Minimize Global Variables
Minimize Cross-File Changes
Other Ways to Access Globals
Scopes and Nested Functions
Nested Scope Details
Nested Scope Examples
Factory functions
Retaining enclosing scopes’ state with defaults
Nested scopes and lambdas
Scopes versus defaults with loop variables
Arbitrary scope nesting
The nonlocal Statement
nonlocal Basics
nonlocal in Action
Using nonlocal for changes
Boundary cases
Why nonlocal?
Shared state with globals
State with classes (preview)
State with function attributes
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
18. Arguments
Argument-Passing Basics
Arguments and Shared References
Avoiding Mutable Argument Changes
Simulating Output Parameters
Special Argument-Matching Modes
The Basics
Matching Syntax
The Gritty Details
Keyword and Default Examples
Keywords
Defaults
Combining keywords and defaults
Arbitrary Arguments Examples
Collecting arguments
Unpacking arguments
Applying functions generically
The defunct apply built-in (Python 2.6)
Python 3.0 Keyword-Only Arguments
Ordering rules
Why keyword-only arguments?
The min Wakeup Call!
Full Credit
Bonus Points
The Punch Line...
Generalized Set Functions
Emulating the Python 3.0 print Function
Using Keyword-Only Arguments
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
19. Advanced Function Topics
Function Design Concepts
Recursive Functions
Summation with Recursion
Coding Alternatives
Loop Statements Versus Recursion
Handling Arbitrary Structures
Function Objects: Attributes and Annotations
Indirect Function Calls
Function Introspection
Function Attributes
Function Annotations in 3.0
Anonymous Functions: lambda
lambda Basics
Why Use lambda?
How (Not) to Obfuscate Your Python Code
Nested lambdas and Scopes
Mapping Functions over Sequences: map
Functional Programming Tools: filter and reduce
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
20. Iterations and Comprehensions, Part 2
List Comprehensions Revisited: Functional Tools
List Comprehensions Versus map
Adding Tests and Nested Loops: filter
List Comprehensions and Matrixes
Comprehending List Comprehensions
Iterators Revisited: Generators
Generator Functions: yield Versus return
State suspension
Iteration protocol integration
Generator functions in action
Extended generator function protocol: send versus next
Generator Expressions: Iterators Meet Comprehensions
Generator Functions Versus Generator Expressions
Generators Are Single-Iterator Objects
Emulating zip and map with Iteration Tools
Coding your own map(func, ...)
Coding your own zip(...) and map(None, ...)
Value Generation in Built-in Types and Classes
3.0 Comprehension Syntax Summary
Comprehending Set and Dictionary Comprehensions
Extended Comprehension Syntax for Sets and Dictionaries
Timing Iteration Alternatives
Timing Module
Timing Script
Timing Results
Timing Module Alternatives
Using keyword-only arguments in 3.0
Other Suggestions
Function Gotchas
Local Names Are Detected Statically
Defaults and Mutable Objects
Functions Without returns
Enclosing Scope Loop Variables
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part IV Exercises
V. Modules
21. Modules: The Big Picture
Why Use Modules?
Python Program Architecture
How to Structure a Program
Imports and Attributes
Standard Library Modules
How Imports Work
1. Find It
2. Compile It (Maybe)
3. Run It
The Module Search Path
Configuring the Search Path
Search Path Variations
The sys.path List
Module File Selection
Advanced Module Selection Concepts
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
22. Module Coding Basics
Module Creation
Module Usage
The import Statement
The from Statement
The from * Statement
Imports Happen Only Once
import and from Are Assignments
Cross-File Name Changes
import and from Equivalence
Potential Pitfalls of the from Statement
When import is required
Module Namespaces
Files Generate Namespaces
Attribute Name Qualification
Imports Versus Scopes
Namespace Nesting
Reloading Modules
reload Basics
reload Example
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
23. Module Packages
Package Import Basics
Packages and Search Path Settings
Package __init__.py Files
Package Import Example
from Versus import with Packages
Why Use Package Imports?
A Tale of Three Systems
Package Relative Imports
Changes in Python 3.0
Relative Import Basics
Why Relative Imports?
The relative imports solution in 3.0
Relative imports versus absolute package paths
The Scope of Relative Imports
Module Lookup Rules Summary
Relative Imports in Action
Imports outside packages
Imports within packages
Imports are still relative to the CWD
Selecting modules with relative and absolute imports
Imports are still relative to the CWD (again)
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
24. Advanced Module Topics
Data Hiding in Modules
Minimizing from * Damage: _X and __all__
Enabling Future Language Features
Mixed Usage Modes: __name__ and __main__
Unit Tests with __name__
Using Command-Line Arguments with __name__
Changing the Module Search Path
The as Extension for import and from
Modules Are Objects: Metaprograms
Importing Modules by Name String
Transitive Module Reloads
Module Design Concepts
Module Gotchas
Statement Order Matters in Top-Level Code
from Copies Names but Doesn’t Link
from * Can Obscure the Meaning of Variables
reload May Not Impact from Imports
reload, from, and Interactive Testing
Recursive from Imports May Not Work
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part V Exercises
VI. Classes and OOP
25. OOP: The Big Picture
Why Use Classes?
OOP from 30,000 Feet
Attribute Inheritance Search
Classes and Instances
Class Method Calls
Coding Class Trees
OOP Is About Code Reuse
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
26. Class Coding Basics
Classes Generate Multiple Instance Objects
Class Objects Provide Default Behavior
Instance Objects Are Concrete Items
A First Example
Classes Are Customized by Inheritance
A Second Example
Classes Are Attributes in Modules
Classes Can Intercept Python Operators
A Third Example
Why Use Operator Overloading?
The World’s Simplest Python Class
Classes Versus Dictionaries
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
27. A More Realistic Example
Step 1: Making Instances
Coding Constructors
Testing As You Go
Using Code Two Ways
Step 2: Adding Behavior Methods
Coding Methods
Step 3: Operator Overloading
Providing Print Displays
Step 4: Customizing Behavior by Subclassing
Coding Subclasses
Augmenting Methods: The Bad Way
Augmenting Methods: The Good Way
Polymorphism in Action
Inherit, Customize, and Extend
OOP: The Big Idea
Step 5: Customizing Constructors, Too
OOP Is Simpler Than You May Think
Other Ways to Combine Classes
Step 6: Using Introspection Tools
Special Class Attributes
A Generic Display Tool
Instance Versus Class Attributes
Name Considerations in Tool Classes
Our Classes’ Final Form
Step 7 (Final): Storing Objects in a Database
Pickles and Shelves
Storing Objects on a Shelve Database
Exploring Shelves Interactively
Updating Objects on a Shelve
Future Directions
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
28. Class Coding Details
The class Statement
General Form
Example
Methods
Method Example
Calling Superclass Constructors
Other Method Call Possibilities
Inheritance
Attribute Tree Construction
Specializing Inherited Methods
Class Interface Techniques
Abstract Superclasses
Python 2.6 and 3.0 Abstract Superclasses
Namespaces: The Whole Story
Simple Names: Global Unless Assigned
Attribute Names: Object Namespaces
The “Zen” of Python Namespaces: Assignments Classify Names
Namespace Dictionaries
Namespace Links
Documentation Strings Revisited
Classes Versus Modules
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
29. Operator Overloading
The Basics
Constructors and Expressions: __init__ and __sub__
Common Operator Overloading Methods
Indexing and Slicing: __getitem__ and __setitem__
Intercepting Slices
Index Iteration: __getitem__
Iterator Objects: __iter__ and __next__
User-Defined Iterators
Multiple Iterators on One Object
Membership: __contains__, __iter__, and __getitem__
Attribute Reference: __getattr__ and __setattr__
Other Attribute Management Tools
Emulating Privacy for Instance Attributes: Part 1
String Representation: __repr__ and __str__
Right-Side and In-Place Addition: __radd__ and __iadd__
In-Place Addition
Call Expressions: __call__
Function Interfaces and Callback-Based Code
Comparisons: __lt__, __gt__, and Others
The 2.6 __cmp__ Method (Removed in 3.0)
Boolean Tests: __bool__ and __len__
Object Destruction: __del__
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
30. Designing with Classes
Python and OOP
Overloading by Call Signatures (or Not)
OOP and Inheritance: “Is-a” Relationships
OOP and Composition: “Has-a” Relationships
Stream Processors Revisited
OOP and Delegation: “Wrapper” Objects
Pseudoprivate Class Attributes
Name Mangling Overview
Why Use Pseudoprivate Attributes?
Methods Are Objects: Bound or Unbound
Unbound Methods are Functions in 3.0
Bound Methods and Other Callable Objects
Multiple Inheritance: “Mix-in” Classes
Coding Mix-in Display Classes
Listing instance attributes with __dict__
Listing inherited attributes with dir
Listing attributes per object in class trees
Classes Are Objects: Generic Object Factories
Why Factories?
Other Design-Related Topics
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
31. Advanced Class Topics
Extending Built-in Types
Extending Types by Embedding
Extending Types by Subclassing
The “New-Style” Class Model
New-Style Class Changes
Type Model Changes
Implications for type testing
All objects derive from “object”
Diamond Inheritance Change
Diamond inheritance example
Explicit conflict resolution
Scope of search order change
New-Style Class Extensions
Instance Slots
Slots and generic code
Multiple __slot__ lists in superclasses
Class Properties
__getattribute__ and Descriptors
Metaclasses
Static and Class Methods
Why the Special Methods?
Static Methods in 2.6 and 3.0
Static Method Alternatives
Using Static and Class Methods
Counting Instances with Static Methods
Counting Instances with Class Methods
Counting instances per class with class methods
Decorators and Metaclasses: Part 1
Function Decorator Basics
A First Function Decorator Example
Class Decorators and Metaclasses
For More Details
Class Gotchas
Changing Class Attributes Can Have Side Effects
Changing Mutable Class Attributes Can Have Side Effects, Too
Multiple Inheritance: Order Matters
Methods, Classes, and Nested Scopes
Delegation-Based Classes in 3.0: __getattr__ and built-ins
“Overwrapping-itis”
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part VI Exercises
VII. Exceptions and Tools
32. Exception Basics
Why Use Exceptions?
Exception Roles
Exceptions: The Short Story
Default Exception Handler
Catching Exceptions
Raising Exceptions
User-Defined Exceptions
Termination Actions
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
33. Exception Coding Details
The try/except/else Statement
try Statement Clauses
The try else Clause
Example: Default Behavior
Example: Catching Built-in Exceptions
The try/finally Statement
Example: Coding Termination Actions with try/finally
Unified try/except/finally
Unified try Statement Syntax
Combining finally and except by Nesting
Unified try Example
The raise Statement
Propagating Exceptions with raise
Python 3.0 Exception Chaining: raise from
The assert Statement
Example: Trapping Constraints (but Not Errors!)
with/as Context Managers
Basic Usage
The Context Management Protocol
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
34. Exception Objects
Exceptions: Back to the Future
String Exceptions Are Right Out!
Class-Based Exceptions
Coding Exceptions Classes
Why Exception Hierarchies?
Built-in Exception Classes
Built-in Exception Categories
Default Printing and State
Custom Print Displays
Custom Data and Behavior
Providing Exception Details
Providing Exception Methods
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
35. Designing with Exceptions
Nesting Exception Handlers
Example: Control-Flow Nesting
Example: Syntactic Nesting
Exception Idioms
Exceptions Aren’t Always Errors
Functions Can Signal Conditions with raise
Closing Files and Server Connections
Debugging with Outer try Statements
Running In-Process Tests
More on sys.exc_info
Exception Design Tips and Gotchas
What Should Be Wrapped
Catching Too Much: Avoid Empty except and Exception
Catching Too Little: Use Class-Based Categories
Core Language Summary
The Python Toolset
Development Tools for Larger Projects
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part VII Exercises
VIII. Advanced Topics
36. Unicode and Byte Strings
String Changes in 3.0
String Basics
Character Encoding Schemes
Python’s String Types
Text and Binary Files
Python 3.0 Strings in Action
Literals and Basic Properties
Conversions
Coding Unicode Strings
Coding ASCII Text
Coding Non-ASCII Text
Encoding and Decoding Non-ASCII text
Other Unicode Coding Techniques
Converting Encodings
Coding Unicode Strings in Python 2.6
Source File Character Set Encoding Declarations
Using 3.0 Bytes Objects
Method Calls
Sequence Operations
Other Ways to Make bytes Objects
Mixing String Types
Using 3.0 (and 2.6) bytearray Objects
Using Text and Binary Files
Text File Basics
Text and Binary Modes in 3.0
Type and Content Mismatches
Using Unicode Files
Reading and Writing Unicode in 3.0
Manual encoding
File output encoding
File input decoding
Decoding mismatches
Handling the BOM in 3.0
Unicode Files in 2.6
Other String Tool Changes in 3.0
The re Pattern Matching Module
The struct Binary Data Module
The pickle Object Serialization Module
XML Parsing Tools
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
37. Managed Attributes
Why Manage Attributes?
Inserting Code to Run on Attribute Access
Properties
The Basics
A First Example
Computed Attributes
Coding Properties with Decorators
Descriptors
The Basics
Descriptor method arguments
Read-only descriptors
A First Example
Computed Attributes
Using State Information in Descriptors
How Properties and Descriptors Relate
__getattr__ and __getattribute__
The Basics
Avoiding loops in attribute interception methods
A First Example
Computed Attributes
__getattr__ and __getattribute__ Compared
Management Techniques Compared
Intercepting Built-in Operation Attributes
Delegation-Based Managers Revisited
Example: Attribute Validations
Using Properties to Validate
Self-test code
Using Descriptors to Validate
Using __getattr__ to Validate
Using __getattribute__ to Validate
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
38. Decorators
What’s a Decorator?
Managing Calls and Instances
Managing Functions and Classes
Using and Defining Decorators
Why Decorators?
The Basics
Function Decorators
Usage
Implementation
Supporting method decoration
Class Decorators
Usage
Implementation
Supporting multiple instances
Decorator Nesting
Decorator Arguments
Decorators Manage Functions and Classes, Too
Coding Function Decorators
Tracing Calls
State Information Retention Options
Class instance attributes
Enclosing scopes and globals
Enclosing scopes and nonlocals
Function attributes
Class Blunders I: Decorating Class Methods
Using nested functions to decorate methods
Using descriptors to decorate methods
Timing Calls
Adding Decorator Arguments
Coding Class Decorators
Singleton Classes
Tracing Object Interfaces
Class Blunders II: Retaining Multiple Instances
Decorators Versus Manager Functions
Why Decorators? (Revisited)
Managing Functions and Classes Directly
Example: “Private” and “Public” Attributes
Implementing Private Attributes
Implementation Details I
Inheritance versus delegation
Decorator arguments
State retention and enclosing scopes
Using __dict__ and __slots__
Generalizing for Public Declarations, Too
Implementation Details II
Using __X pseudoprivate names
Breaking privacy
Decorator tradeoffs
Open Issues
Caveat: operator overloading methods fail to delegate under 3.0
Implementation alternatives: __getattribute__ inserts, call stack inspection
Python Isn’t About Control
Example: Validating Function Arguments
The Goal
A Basic Range-Testing Decorator for Positional Arguments
Generalizing for Keywords and Defaults, Too
Implementation Details
Function introspection
Argument assumptions
Matching algorithm
Open Issues
Decorator Arguments Versus Function Annotations
Other Applications: Type Testing (If You Insist!)
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
39. Metaclasses
To Metaclass or Not to Metaclass
Increasing Levels of Magic
The Downside of “Helper” Functions
Metaclasses Versus Class Decorators: Round 1
The Metaclass Model
Classes Are Instances of type
Metaclasses Are Subclasses of Type
Class Statement Protocol
Declaring Metaclasses
Coding Metaclasses
A Basic Metaclass
Customizing Construction and Initialization
Other Metaclass Coding Techniques
Using simple factory functions
Overloading class creation calls with metaclasses
Overloading class creation calls with normal classes
Instances Versus Inheritance
Example: Adding Methods to Classes
Manual Augmentation
Metaclass-Based Augmentation
Metaclasses Versus Class Decorators: Round 2
Decorator-based augmentation
Managing instances instead of classes
Example: Applying Decorators to Methods
Tracing with Decoration Manually
Tracing with Metaclasses and Decorators
Applying Any Decorator to Methods
Metaclasses Versus Class Decorators: Round 3
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
IX. Appendixes
A. Installation and Configuration
Installing the Python Interpreter
Is Python Already Present?
Where to Get Python
Installation Steps
Configuring Python
Python Environment Variables
How to Set Configuration Options
Unix/Linux shell variables
DOS variables (Windows)
Windows environment variable GUI
Windows registry
Path files
Python Command-Line Options
For More Help
B. Solutions to End-of-Part Exercises
Part I, Getting Started
Part II, Types and Operations
Part III, Statements and Syntax
Part IV, Functions
Part V, Modules
Part VI, Classes and OOP
Part VII, Exceptions and Tools
Index
About the Author
Colophon
Copyright
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
35. Designing with Exceptions
Next
Next Chapter
36. Unicode and Byte Strings
Part VIII. Advanced Topics
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset