Tải bản đầy đủ (.pdf) (1,594 trang)

Mark lutz learning python, 5th edition 2013 kho tài liệu bách khoa

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (14.46 MB, 1,594 trang )



FIFTH EDITION

Download from Wow! eBook <www.wowebook.com>

Learning Python

Mark Lutz

Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo


Learning Python, Fifth Edition
by Mark Lutz
Copyright © 2013 Mark Lutz. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (). For more information, contact our
corporate/institutional sales department: 800-998-9938 or

Editor: Rachel Roumeliotis
Production Editor: Christopher Hearse
Copyeditor: Rachel Monaghan
Proofreader: Julie Van Keuren
June 2013:

Indexer: Lucie Haskins
Cover Designer: Randy Comer
Interior Designer: David Futato


Illustrator: Rebecca Demarest

Fifth Edition.

Revision History for the Fifth Edition:
2013-06-07
First release
See for release details.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Learning Python, 5th Edition, the image of a wood rat, and related trade dress are
trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

ISBN: 978-1-449-35573-9
[QG]
1370970520


To Vera.
You are my life.



Table of Contents


Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii

Part I. Getting Started
1. A Python Q&A Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
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
And More: Gaming, Images, Data Mining, Robots, Excel...
How Is Python Developed and Supported?
Open Source Tradeoffs
What Are 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


3
4
5
5
7
9
10
11
11
11
12
12
13
13
14
15
15
16
16
17
17
18
19
19
20
20
v



How Does Python Stack Up to Language X?
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

21
22
23
23

2. How Python Runs Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Introducing the Python Interpreter
Program Execution
The Programmer’s View
Python’s View
Execution Model Variations
Python Implementation Alternatives
Execution Optimization Tools
Frozen Binaries
Future Possibilities?
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

27
28
28
30
33
33

37
39
40
40
41
41

3. How You Run Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
The Interactive Prompt
Starting 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 Files
A 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 Icons
Icon-Click Basics
Clicking Icons on Windows
The input Trick on Windows

Other Icon-Click Limitations
vi | Table of Contents

43
44
45
46
47
48
49
50
52
54
55
56
57
58
59
59
60
60
62
62
63
63
66


Module Imports and Reloads
Import and Reload Basics

The Grander Module Story: Attributes
Usage Notes: import and reload
Using exec to Run Module Files
The IDLE User Interface
IDLE Startup Details
IDLE Basic Usage
IDLE Usability Features
Advanced IDLE Tools
Usage Notes: IDLE
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

66
66
68
71
72
73
74
75

76
77
78
79
81
81
82
82
82
83
83
85
85
86
87

Part II. Types and Operations
4. Introducing Python Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The 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

Lists
Sequence Operations
Type-Specific Operations

93
94
95
97
99
99
101
102
104
105
106
108
109
109
109

Table of Contents | vii


Bounds Checking
Nesting
Comprehensions
Dictionaries
Mapping Operations
Nesting Revisited
Missing Keys: if Tests

Sorting Keys: for Loops
Iteration and Optimization
Tuples
Why Tuples?
Files
Binary Bytes Files
Unicode Text 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

110
110
111
113
114
115
116
118
120
121
122
122
123
124

126
126
128
129
130
130
131
131

5. Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
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
Hex, Octal, Binary: Literals and Conversions
Bitwise Operations
Other Built-in Numeric Tools
Other Numeric Types
Decimal Type
Fraction Type
Sets
Booleans


viii | Table of Contents

133
134
136
136
141
141
143
144
146
150
151
151
153
155
157
157
160
163
171


Numeric Extensions
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

172
172

173
173

6. The Dynamic Typing Interlude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
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

175
176
177
178
180
181
183
185
186
186
186

7. String Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
This Chapter’s Scope

Unicode: The Short Story
String Basics
String Literals
Single- and Double-Quoted Strings Are the Same
Escape Sequences Represent Special Characters
Raw Strings Suppress Escapes
Triple Quotes Code Multiline Block Strings
Strings in Action
Basic Operations
Indexing and Slicing
String Conversion Tools
Changing Strings I
String Methods
Method 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 Expressions
Formatting Expression Basics
Advanced Formatting Expression Syntax
Advanced Formatting Expression Examples

189
189
190
192
193
193

196
198
200
200
201
205
208
209
209
210
211
213
214
215
216
217
218
220
Table of Contents | ix


Dictionary-Based Formatting Expressions
String Formatting Method Calls
Formatting 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 Categories

Types Share Operation Sets by Categories
Mutable Types Can Be Changed in Place
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

221
222
222
223
224
225
227
230
235
235
236
237
237
237

8. Lists and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
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
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

239
242
242
242
243
244
250
252
253
254
254
256
258
262
264
271
272
272


9. Tuples, Files, and Everything Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Tuples
Tuples in Action
Why Lists and Tuples?
Records Revisited: Named Tuples
Files
Opening Files
Using Files
x | Table of Contents

276
277
279
280
282
283
284


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 Summary
Object 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 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

285
287
288
290
291
293
294
294
295
297
297
300
304
306
306

308
308
308
309
310
311
311
311
312
313

Part III. Statements and Syntax
10. Introducing Python Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
The Python Conceptual Hierarchy 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

319
320
322

322
323
324
327
329
329
331
332
333
335

Table of Contents | xi


Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

336
336
336

11. Assignments, Expressions, and Prints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Assignment Statements
Assignment Statement Forms
Sequence Assignments
Extended Sequence Unpacking in Python 3.X
Multiple-Target Assignments
Augmented Assignments
Variable Name Rules

Expression Statements
Expression Statements and In-Place Changes
Print Operations
The 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

339
340
341
344
348
350
352
356
357
358
359
361
363
366
369
370
370

12. if Tests and Syntax Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

if Statements
General Format
Basic Examples
Multiway Branching
Python Syntax Revisited
Block 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

371
371
372
372
375
376
378
379
380
382
385
385
386

13. while and for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
while Loops

General Format
Examples
break, continue, pass, and the Loop else
xii | Table of Contents

387
388
388
389


General Loop Format
pass
continue
break
Loop else
for Loops
General Format
Examples
Loop Coding Techniques
Counter 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


389
390
391
391
392
395
395
395
402
402
403
404
405
406
407
410
413
414
414

14. Iterations and Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Iterations: A First Look
The Iteration Protocol: File Iterators
Manual Iteration: iter and next
Other Built-in Type Iterables
List Comprehensions: A First Detailed Look
List Comprehension Basics
Using List Comprehensions on Files
Extended List Comprehension Syntax

Other Iteration Contexts
New Iterables in Python 3.X
Impacts 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

416
416
419
422
424
425
426
427
429
434
434
435
436
438
439
440
441
441

441

15. The Documentation Interlude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Python Documentation Sources

443
Table of Contents | xiii


# 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

444
444
446
449
452
461

461
462
463
463
465
466
466
467

Part IV. Functions and Generators
16. Function Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
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

474
475

476
477
478
478
478
479
480
481
481
482
483
483
483
484

17. Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Python Scope Basics
Scope Details
Name Resolution: The LEGB Rule
Scope Example
The Built-in Scope
The global Statement

xiv | Table of Contents

485
486
488
490
491

494


Program Design: Minimize Global Variables
Program Design: Minimize Cross-File Changes
Other Ways to Access Globals
Scopes and Nested Functions
Nested Scope Details
Nested Scope Examples
Factory Functions: Closures
Retaining Enclosing Scope State with Defaults
The nonlocal Statement in 3.X
nonlocal Basics
nonlocal in Action
Why nonlocal? State Retention Options
State 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

495
497
498
499
500
500
501

504
508
508
509
512
512
513
513
515
519
519
520

18. Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Argument-Passing Basics
Arguments and Shared References
Avoiding Mutable Argument Changes
Simulating Output Parameters and Multiple Results
Special Argument-Matching Modes
Argument 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 Function
Using Keyword-Only Arguments
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

523
524
526
527
528
529
530
531
532
534
539
542
542
544
544
545
547
548
550
551
552

Table of Contents | xv



19. Advanced Function Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
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: “First Class” Objects
Function Introspection
Function Attributes
Function Annotations in 3.X
Anonymous Functions: lambda
lambda Basics
Why Use lambda?
How (Not) to Obfuscate Your Python Code
Scopes: lambdas Can Be Nested Too
Functional Programming Tools
Mapping Functions over Iterables: map
Selecting Items in Iterables: filter
Combining Items in Iterables: reduce
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

553
555
555
556

557
558
562
562
563
564
565
567
568
569
571
572
574
574
576
576
578
578
578

20. Comprehensions and Generations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
List Comprehensions and Functional Tools
List Comprehensions Versus map
Adding Tests and Nested Loops: filter
Example: List Comprehensions and Matrixes
Don’t Abuse List Comprehensions: KISS
Generator Functions and Expressions
Generator 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 Summary
Scopes and Comprehension Variables
Comprehending Set and Dictionary Comprehensions
xvi | Table of Contents

581
582
583
586
588
591
592
597
602
604
606
609
614
617
622
623
624


Download from Wow! eBook <www.wowebook.com>


Extended Comprehension Syntax for Sets and Dictionaries
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

625
626
626
626

21. The Benchmarking Interlude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Timing Iteration Alternatives
Timing Module: Homegrown
Timing Script
Timing Results
Timing Module Alternatives
Other Suggestions
Timing Iterations and Pythons with timeit
Basic timeit Usage
Benchmark Module and Script: timeit
Benchmark Script Results
More Fun with Benchmarks
Other Benchmarking Topics: pystones
Function Gotchas
Local 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

629
630
634
635
638
642
642
643
647
649
651
656
656
657
658
660
661
661
662
662
663

Part V. Modules and Packages
22. Modules: The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
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
Byte Code Files: __pycache__ in Python 3.2+
Byte Code File Models in Action

669
670
671
671
673
674
674
675
675
676
677

Table of Contents | xvii


The Module Search Path
Configuring the Search Path
Search Path Variations
The sys.path List
Module File Selection

Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

678
681
681
681
682
685
685
685

23. Module Coding Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Module Creation
Module Filenames
Other Kinds of Modules
Module Usage
The 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 Namespaces
Files Generate Namespaces
Namespace Dictionaries: __dict__
Attribute Name Qualification
Imports Versus Scopes

Namespace Nesting
Reloading Modules
reload Basics
reload Example
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

687
687
688
688
689
689
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704

704

24. Module Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
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
xviii | Table of Contents

708
708
709
711
713
713
714
717


Changes 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 Packages
Namespace 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

718
718
720
722
723
723
729
734
735
736
737
738
740
742
742
742

25. Advanced Module Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
Module Design Concepts
Data Hiding in Modules

Minimizing from * Damage: _X and __all__
Enabling Future Language Features: __future__
Mixed Usage Modes: __name__ and __main__
Unit Tests with __name__
Example: Dual Mode Code
Currency 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 String
Running Code Strings
Direct Calls: Two Options
Example: Transitive Module Reloads
A Recursive Reloader
Alternative Codings
Module Gotchas
Module 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

745
747
747
748
749
750

751
754
756
756
758
759
761
762
762
763
764
767
770
771
771
772
773
773
774

Table of Contents | xix


Recursive from Imports May Not Work
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part V Exercises

775

776
777
777
778

Part VI. Classes and OOP
26. OOP: The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Why Use Classes?
OOP from 30,000 Feet
Attribute 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

784
785
785
788
788
789
791
792
795
795
795


27. Class Coding Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
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
Records Revisited: Classes Versus Dictionaries
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

797
798
798
799
801
802
804
805
806
808
809
812

814
815
815

28. A More Realistic Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
Step 1: Making Instances
Coding Constructors
Testing As You Go

xx | Table of Contents

818
818
819


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

820
822
824
826
826
828
828
829
829
832
833
833
834

836
836
840
840
842
843
844
845
847
847
848
849
851
853
855
855
856

29. Class Coding Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
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


859
860
860
862
863
864
864
865
865
866
867

Table of Contents | xxi


Abstract Superclasses
Namespaces: The Conclusion
Simple 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


869
872
872
872
873
875
878
880
882
884
884
884
885

30. Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887
The Basics
Constructors 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 Others
The __cmp__ Method in Python 2.X

xxii | Table of Contents

887
888
888
890
891
893
894
894
895
896
899
902
906
909

909
910
912
912
913
914
916
917
917
920
921
923
925
926


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

927
928
929
930
931
931

931

31. Designing with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
Python and OOP
Polymorphism Means Interfaces, Not Call Signatures
OOP and Inheritance: “Is-a” Relationships
OOP and Composition: “Has-a” Relationships
Stream Processors Revisited
OOP and Delegation: “Wrapper” Proxy Objects
Pseudoprivate Class Attributes
Name Mangling Overview
Why Use Pseudoprivate Attributes?
Methods Are Objects: Bound or Unbound
Unbound Methods Are Functions in 3.X
Bound Methods and Other Callable Objects
Classes Are Objects: Generic Object Factories
Why Factories?
Multiple Inheritance: “Mix-in” Classes
Coding Mix-in Display Classes
Other Design-Related Topics
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers

933
934
935
937
938
942

944
945
945
948
950
951
954
955
956
957
977
977
978
978

32. Advanced Class Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
Extending Built-in Types
Extending Types by Embedding
Extending Types by Subclassing
The “New Style” Class Model
Just How New Is New-Style?
New-Style Class Changes
Attribute 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

980

980
981
983
984
985
987
992
995
997
1001
1004
Table of Contents | xxiii


×