Learning Python

Table of Contents

SPECIAL OFFER: Upgrade this ebook with O’Reilly
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
Execution Model Variations
Python Implementation Alternatives
Execution Optimization Tools
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?
Using the Interactive Prompt
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
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
Numbers in Action
Variables and Basic Expressions
Numeric Display Formats
Comparisons: Normal and Chained
Division: Classic, Floor, and True
Integer Precision
Complex Numbers
Hexadecimal, Octal, and Binary Notation
Bitwise Operations
Other Built-in Numeric Tools
Other Numeric Types
Decimal Type
Fraction Type
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
String Conversion Tools
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?
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
Dictionaries
Dictionaries in Action
Basic Dictionary Operations
Changing Dictionaries In-Place
More Dictionary Methods
A Languages Table
Dictionary Usage Notes
Other Ways to Make Dictionaries
Dictionary Changes in Python 3.0
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
9. Tuples, Files, and Everything Else
Tuples
Tuples in Action
Why Lists and Tuples?
Files
Opening Files
Using Files
Files in Action
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
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
Why Indentation Syntax?
A Few Special Cases
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
Extended Sequence Unpacking in Python 3.0
Multiple-Target Assignments
Augmented Assignments
Variable Name Rules
Expression Statements
Expression Statements and In-Place Changes
Print Operations
The Python 3.0 print Function
The Python 2.6 print Statement
Print 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
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
for Loops
General Format
Examples
Loop Coding Techniques
Counter Loops: while and range
Nonexhaustive Traversals: range and Slices
Changing Lists: range
Parallel Traversals: zip and map
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__
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
The nonlocal Statement
nonlocal Basics
nonlocal in Action
Why nonlocal?
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
Arbitrary Arguments Examples
Python 3.0 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
Generator Expressions: Iterators Meet Comprehensions
Generator Functions Versus Generator Expressions
Generators Are Single-Iterator Objects
Emulating zip and map with Iteration Tools
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
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
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 Scope of Relative Imports
Module Lookup Rules Summary
Relative Imports in Action
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
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
Diamond Inheritance Change
New-Style Class Extensions
Instance Slots
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
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
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
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
Delegation-Based Managers Revisited
Example: Attribute Validations
Using Properties to Validate
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
Class Decorators
Decorator Nesting
Decorator Arguments
Decorators Manage Functions and Classes, Too
Coding Function Decorators
Tracing Calls
State Information Retention Options
Class Blunders I: Decorating Class 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
Generalizing for Public Declarations, Too
Implementation Details II
Open Issues
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
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
Instances Versus Inheritance
Example: Adding Methods to Classes
Manual Augmentation
Metaclass-Based Augmentation
Metaclasses Versus Class Decorators: Round 2
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
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
SPECIAL OFFER: Upgrade this ebook with O’Reilly