100% found this document useful (8 votes)
247 views

Download Learning Python 4th edition Mark Lutz ebook All Chapters PDF

Learning

Uploaded by

acnamajada46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (8 votes)
247 views

Download Learning Python 4th edition Mark Lutz ebook All Chapters PDF

Learning

Uploaded by

acnamajada46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Download the full version of the ebook at ebookname.

com

Learning Python 4th edition Mark Lutz

https://ebookname.com/product/learning-python-4th-edition-
mark-lutz/

OR CLICK BUTTON

DOWNLOAD EBOOK

Download more ebook instantly today at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Learning Python Third Edition Mark Lutz

https://ebookname.com/product/learning-python-third-edition-mark-lutz/

ebookname.com

Programming Python 4th Edition Mark Lutz

https://ebookname.com/product/programming-python-4th-edition-mark-
lutz/

ebookname.com

Python Pocket Reference 4th ed Edition Mark Lutz

https://ebookname.com/product/python-pocket-reference-4th-ed-edition-
mark-lutz/

ebookname.com

Introduction to Operations Research 7th Bk& Cdr Edition


Sheldon M. Ross

https://ebookname.com/product/introduction-to-operations-research-7th-
bk-cdr-edition-sheldon-m-ross/

ebookname.com
Demystifying Global Macroeconomics 3rd Edition Edition
John E. Marthinsen

https://ebookname.com/product/demystifying-global-macroeconomics-3rd-
edition-edition-john-e-marthinsen/

ebookname.com

Presenting Your Findings A Practical Guide for Creating


Tables 6th Edition Adelheid A. M. Nicol

https://ebookname.com/product/presenting-your-findings-a-practical-
guide-for-creating-tables-6th-edition-adelheid-a-m-nicol/

ebookname.com

Prophecy and Reason The Dutch Collegiants in the Early


Enlightenment Andrew Cooper Fix

https://ebookname.com/product/prophecy-and-reason-the-dutch-
collegiants-in-the-early-enlightenment-andrew-cooper-fix/

ebookname.com

The Blackwell Companion to Social Work 4th Edition Martin


Davies

https://ebookname.com/product/the-blackwell-companion-to-social-
work-4th-edition-martin-davies/

ebookname.com

Contemporary Cognitive Therapy Theory Research and


Practice 1st Edition Robert L. Leahy Phd

https://ebookname.com/product/contemporary-cognitive-therapy-theory-
research-and-practice-1st-edition-robert-l-leahy-phd/

ebookname.com
An Enquiry into Bukharan Qadimism Mirza Salim bik 1st
Edition Franz Wennberg

https://ebookname.com/product/an-enquiry-into-bukharan-qadimism-mirza-
salim-bik-1st-edition-franz-wennberg/

ebookname.com
Learning Python
FOURTH EDITION

Learning Python

Mark Lutz

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


Learning Python, Fourth Edition
by Mark Lutz

Copyright © 2009 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 (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.

Editor: Julie Steele Indexer: John Bickelhaupt


Production Editor: Sumita Mukherji Cover Designer: Karen Montgomery
Copyeditor: Rachel Head Interior Designer: David Futato
Production Services: Newgen North America Illustrator: Robert Romano

Printing History:
March 1999: First Edition.
December 2003: Second Edition.
October 2007: Third Edition.
September 2009: Fourth Edition.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Learning Python, 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 author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.

ISBN: 978-0-596-15806-4

[M]

1252944666
To Vera.
You are my life.
Table of Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi

Part I. Getting Started

1. A Python Q&A Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


Why Do People Use Python? 3
Software Quality 4
Developer Productivity 5
Is Python a “Scripting Language”? 5
OK, but What’s the Downside? 7
Who Uses Python Today? 7
What Can I Do with Python? 9
Systems Programming 9
GUIs 9
Internet Scripting 10
Component Integration 10
Database Programming 11
Rapid Prototyping 11
Numeric and Scientific Programming 11
Gaming, Images, Serial Ports, XML, Robots, and More 12
How Is Python Supported? 12
What Are Python’s Technical Strengths? 13
It’s Object-Oriented 13
It’s Free 13
It’s Portable 14
It’s Powerful 15
It’s Mixable 16
It’s Easy to Use 16
It’s Easy to Learn 17
It’s Named After Monty Python 17
How Does Python Stack Up to Language X? 17

vii
Chapter Summary 18
Test Your Knowledge: Quiz 19
Test Your Knowledge: Answers 19

2. How Python Runs Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23


Introducing the Python Interpreter 23
Program Execution 24
The Programmer’s View 24
Python’s View 26
Execution Model Variations 29
Python Implementation Alternatives 29
Execution Optimization Tools 30
Frozen Binaries 32
Other Execution Options 33
Future Possibilities? 33
Chapter Summary 34
Test Your Knowledge: Quiz 34
Test Your Knowledge: Answers 34

3. How You Run Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35


The Interactive Prompt 35
Running Code Interactively 37
Why the Interactive Prompt? 38
Using the Interactive Prompt 39
System Command Lines and Files 41
A First Script 42
Running Files with Command Lines 43
Using Command Lines and Files 44
Unix Executable Scripts (#!) 46
Clicking File Icons 47
Clicking Icons on Windows 47
The input Trick 49
Other Icon-Click Limitations 50
Module Imports and Reloads 51
The Grander Module Story: Attributes 53
import and reload Usage Notes 56
Using exec to Run Module Files 57
The IDLE User Interface 58
IDLE Basics 58
Using IDLE 60
Advanced IDLE Tools 62
Other IDEs 63
Other Launch Options 64

viii | Table of Contents


Embedding Calls 64
Frozen Binary Executables 65
Text Editor Launch Options 65
Still Other Launch Options 66
Future Possibilities? 66
Which Option Should I Use? 66
Chapter Summary 68
Test Your Knowledge: Quiz 68
Test Your Knowledge: Answers 69
Test Your Knowledge: Part I Exercises 70

Part II. Types and Operations

4. Introducing Python Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75


Why Use Built-in Types? 76
Python’s Core Data Types 77
Numbers 78
Strings 80
Sequence Operations 80
Immutability 82
Type-Specific Methods 82
Getting Help 84
Other Ways to Code Strings 85
Pattern Matching 85
Lists 86
Sequence Operations 86
Type-Specific Operations 87
Bounds Checking 87
Nesting 88
Comprehensions 88
Dictionaries 90
Mapping Operations 90
Nesting Revisited 91
Sorting Keys: for Loops 93
Iteration and Optimization 94
Missing Keys: if Tests 95
Tuples 96
Why Tuples? 97
Files 97
Other File-Like Tools 99
Other Core Types 99
How to Break Your Code’s Flexibility 100

Table of Contents | ix
User-Defined Classes 101
And Everything Else 102
Chapter Summary 103
Test Your Knowledge: Quiz 103
Test Your Knowledge: Answers 104

5. Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105


Numeric Type Basics 105
Numeric Literals 106
Built-in Numeric Tools 108
Python Expression Operators 108
Numbers in Action 113
Variables and Basic Expressions 113
Numeric Display Formats 115
Comparisons: Normal and Chained 116
Division: Classic, Floor, and True 117
Integer Precision 121
Complex Numbers 122
Hexadecimal, Octal, and Binary Notation 122
Bitwise Operations 124
Other Built-in Numeric Tools 125
Other Numeric Types 127
Decimal Type 127
Fraction Type 129
Sets 133
Booleans 139
Numeric Extensions 140
Chapter Summary 141
Test Your Knowledge: Quiz 141
Test Your Knowledge: Answers 141

6. The Dynamic Typing Interlude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143


The Case of the Missing Declaration Statements 143
Variables, Objects, and References 144
Types Live with Objects, Not Variables 145
Objects Are Garbage-Collected 146
Shared References 148
Shared References and In-Place Changes 149
Shared References and Equality 151
Dynamic Typing Is Everywhere 152
Chapter Summary 153
Test Your Knowledge: Quiz 153
Test Your Knowledge: Answers 154

x | Table of Contents
7. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
String Literals 157
Single- and Double-Quoted Strings Are the Same 158
Escape Sequences Represent Special Bytes 158
Raw Strings Suppress Escapes 161
Triple Quotes Code Multiline Block Strings 162
Strings in Action 163
Basic Operations 164
Indexing and Slicing 165
String Conversion Tools 169
Changing Strings 171
String Methods 172
String Method Examples: Changing Strings 174
String Method Examples: Parsing Text 176
Other Common String Methods in Action 177
The Original string Module (Gone in 3.0) 178
String Formatting Expressions 179
Advanced String Formatting Expressions 181
Dictionary-Based String Formatting Expressions 182
String Formatting Method Calls 183
The Basics 184
Adding Keys, Attributes, and Offsets 184
Adding Specific Formatting 185
Comparison to the % Formatting Expression 187
Why the New Format Method? 190
General Type Categories 193
Types Share Operation Sets by Categories 194
Mutable Types Can Be Changed In-Place 194
Chapter Summary 195
Test Your Knowledge: Quiz 195
Test Your Knowledge: Answers 196

8. Lists and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197


Lists 197
Lists in Action 200
Basic List Operations 200
List Iteration and Comprehensions 200
Indexing, Slicing, and Matrixes 201
Changing Lists In-Place 202
Dictionaries 207
Dictionaries in Action 209
Basic Dictionary Operations 209
Changing Dictionaries In-Place 210

Table of Contents | xi
More Dictionary Methods 211
A Languages Table 212
Dictionary Usage Notes 213
Other Ways to Make Dictionaries 216
Dictionary Changes in Python 3.0 217
Chapter Summary 223
Test Your Knowledge: Quiz 224
Test Your Knowledge: Answers 224

9. Tuples, Files, and Everything Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225


Tuples 225
Tuples in Action 227
Why Lists and Tuples? 229
Files 229
Opening Files 230
Using Files 231
Files in Action 232
Other File Tools 238
Type Categories Revisited 239
Object Flexibility 241
References Versus Copies 241
Comparisons, Equality, and Truth 244
Python 3.0 Dictionary Comparisons 246
The Meaning of True and False in Python 246
Python’s Type Hierarchies 248
Type Objects 249
Other Types in Python 250
Built-in Type Gotchas 251
Assignment Creates References, Not Copies 251
Repetition Adds One Level Deep 252
Beware of Cyclic Data Structures 252
Immutable Types Can’t Be Changed In-Place 253
Chapter Summary 253
Test Your Knowledge: Quiz 254
Test Your Knowledge: Answers 254
Test Your Knowledge: Part II Exercises 255

Part III. Statements and Syntax

10. Introducing Python Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261


Python Program Structure Revisited 261
Python’s Statements 262

xii | Table of Contents


A Tale of Two ifs 264
What Python Adds 264
What Python Removes 265
Why Indentation Syntax? 266
A Few Special Cases 269
A Quick Example: Interactive Loops 271
A Simple Interactive Loop 271
Doing Math on User Inputs 272
Handling Errors by Testing Inputs 273
Handling Errors with try Statements 274
Nesting Code Three Levels Deep 275
Chapter Summary 276
Test Your Knowledge: Quiz 276
Test Your Knowledge: Answers 277

11. Assignments, Expressions, and Prints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279


Assignment Statements 279
Assignment Statement Forms 280
Sequence Assignments 281
Extended Sequence Unpacking in Python 3.0 284
Multiple-Target Assignments 288
Augmented Assignments 289
Variable Name Rules 292
Expression Statements 295
Expression Statements and In-Place Changes 296
Print Operations 297
The Python 3.0 print Function 298
The Python 2.6 print Statement 300
Print Stream Redirection 302
Version-Neutral Printing 306
Chapter Summary 308
Test Your Knowledge: Quiz 308
Test Your Knowledge: Answers 308

12. if Tests and Syntax Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311


if Statements 311
General Format 311
Basic Examples 312
Multiway Branching 312
Python Syntax Rules 314
Block Delimiters: Indentation Rules 315
Statement Delimiters: Lines and Continuations 317
A Few Special Cases 318

Table of Contents | xiii


Truth Tests 320
The if/else Ternary Expression 321
Chapter Summary 324
Test Your Knowledge: Quiz 324
Test Your Knowledge: Answers 324

13. while and for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327


while Loops 327
General Format 328
Examples 328
break, continue, pass, and the Loop else 329
General Loop Format 329
pass 330
continue 331
break 331
Loop else 332
for Loops 334
General Format 334
Examples 335
Loop Coding Techniques 341
Counter Loops: while and range 342
Nonexhaustive Traversals: range and Slices 343
Changing Lists: range 344
Parallel Traversals: zip and map 345
Generating Both Offsets and Items: enumerate 348
Chapter Summary 349
Test Your Knowledge: Quiz 349
Test Your Knowledge: Answers 350

14. Iterations and Comprehensions, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351


Iterators: A First Look 351
The Iteration Protocol: File Iterators 352
Manual Iteration: iter and next 354
Other Built-in Type Iterators 356
List Comprehensions: A First Look 358
List Comprehension Basics 359
Using List Comprehensions on Files 359
Extended List Comprehension Syntax 361
Other Iteration Contexts 362
New Iterables in Python 3.0 366
The range Iterator 367
The map, zip, and filter Iterators 368
Multiple Versus Single Iterators 369

xiv | Table of Contents


Dictionary View Iterators 370
Other Iterator Topics 372
Chapter Summary 372
Test Your Knowledge: Quiz 372
Test Your Knowledge: Answers 373

15. The Documentation Interlude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375


Python Documentation Sources 375
# Comments 376
The dir Function 376
Docstrings: __doc__ 377
PyDoc: The help Function 380
PyDoc: HTML Reports 383
The Standard Manual Set 386
Web Resources 387
Published Books 387
Common Coding Gotchas 387
Chapter Summary 389
Test Your Knowledge: Quiz 389
Test Your Knowledge: Answers 390
Test Your Knowledge: Part III Exercises 390

Part IV. Functions

16. Function Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395


Why Use Functions? 396
Coding Functions 396
def Statements 398
def Executes at Runtime 399
A First Example: Definitions and Calls 400
Definition 400
Calls 400
Polymorphism in Python 401
A Second Example: Intersecting Sequences 402
Definition 402
Calls 403
Polymorphism Revisited 403
Local Variables 404
Chapter Summary 404
Test Your Knowledge: Quiz 405
Test Your Knowledge: Answers 405

Table of Contents | xv
17. Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Python Scope Basics 407
Scope Rules 408
Name Resolution: The LEGB Rule 410
Scope Example 411
The Built-in Scope 412
The global Statement 414
Minimize Global Variables 415
Minimize Cross-File Changes 416
Other Ways to Access Globals 418
Scopes and Nested Functions 419
Nested Scope Details 419
Nested Scope Examples 419
The nonlocal Statement 425
nonlocal Basics 425
nonlocal in Action 426
Why nonlocal? 429
Chapter Summary 432
Test Your Knowledge: Quiz 433
Test Your Knowledge: Answers 434

18. Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435


Argument-Passing Basics 435
Arguments and Shared References 436
Avoiding Mutable Argument Changes 438
Simulating Output Parameters 439
Special Argument-Matching Modes 440
The Basics 441
Matching Syntax 442
The Gritty Details 443
Keyword and Default Examples 444
Arbitrary Arguments Examples 446
Python 3.0 Keyword-Only Arguments 450
The min Wakeup Call! 453
Full Credit 454
Bonus Points 455
The Punch Line... 456
Generalized Set Functions 456
Emulating the Python 3.0 print Function 457
Using Keyword-Only Arguments 459
Chapter Summary 460
Test Your Knowledge: Quiz 461
Test Your Knowledge: Answers 462

xvi | Table of Contents


19. Advanced Function Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Function Design Concepts 463
Recursive Functions 465
Summation with Recursion 465
Coding Alternatives 466
Loop Statements Versus Recursion 467
Handling Arbitrary Structures 468
Function Objects: Attributes and Annotations 469
Indirect Function Calls 469
Function Introspection 470
Function Attributes 471
Function Annotations in 3.0 472
Anonymous Functions: lambda 474
lambda Basics 474
Why Use lambda? 475
How (Not) to Obfuscate Your Python Code 477
Nested lambdas and Scopes 478
Mapping Functions over Sequences: map 479
Functional Programming Tools: filter and reduce 481
Chapter Summary 483
Test Your Knowledge: Quiz 483
Test Your Knowledge: Answers 483

20. Iterations and Comprehensions, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485


List Comprehensions Revisited: Functional Tools 485
List Comprehensions Versus map 486
Adding Tests and Nested Loops: filter 487
List Comprehensions and Matrixes 489
Comprehending List Comprehensions 490
Iterators Revisited: Generators 492
Generator Functions: yield Versus return 492
Generator Expressions: Iterators Meet Comprehensions 497
Generator Functions Versus Generator Expressions 498
Generators Are Single-Iterator Objects 499
Emulating zip and map with Iteration Tools 500
Value Generation in Built-in Types and Classes 506
3.0 Comprehension Syntax Summary 507
Comprehending Set and Dictionary Comprehensions 507
Extended Comprehension Syntax for Sets and Dictionaries 508
Timing Iteration Alternatives 509
Timing Module 509
Timing Script 510
Timing Results 511

Table of Contents | xvii


Timing Module Alternatives 513
Other Suggestions 517
Function Gotchas 518
Local Names Are Detected Statically 518
Defaults and Mutable Objects 520
Functions Without returns 522
Enclosing Scope Loop Variables 522
Chapter Summary 522
Test Your Knowledge: Quiz 523
Test Your Knowledge: Answers 523
Test Your Knowledge: Part IV Exercises 524

Part V. Modules

21. Modules: The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529


Why Use Modules? 529
Python Program Architecture 530
How to Structure a Program 531
Imports and Attributes 531
Standard Library Modules 533
How Imports Work 533
1. Find It 534
2. Compile It (Maybe) 534
3. Run It 535
The Module Search Path 535
Configuring the Search Path 537
Search Path Variations 538
The sys.path List 538
Module File Selection 539
Advanced Module Selection Concepts 540
Chapter Summary 541
Test Your Knowledge: Quiz 541
Test Your Knowledge: Answers 542

22. Module Coding Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543


Module Creation 543
Module Usage 544
The import Statement 544
The from Statement 545
The from * Statement 545
Imports Happen Only Once 546
import and from Are Assignments 546

xviii | Table of Contents


Cross-File Name Changes 547
import and from Equivalence 548
Potential Pitfalls of the from Statement 548
Module Namespaces 550
Files Generate Namespaces 550
Attribute Name Qualification 552
Imports Versus Scopes 552
Namespace Nesting 553
Reloading Modules 554
reload Basics 555
reload Example 556
Chapter Summary 558
Test Your Knowledge: Quiz 558
Test Your Knowledge: Answers 558

23. Module Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561


Package Import Basics 561
Packages and Search Path Settings 562
Package __init__.py Files 563
Package Import Example 564
from Versus import with Packages 566
Why Use Package Imports? 566
A Tale of Three Systems 567
Package Relative Imports 569
Changes in Python 3.0 570
Relative Import Basics 570
Why Relative Imports? 572
The Scope of Relative Imports 574
Module Lookup Rules Summary 575
Relative Imports in Action 575
Chapter Summary 581
Test Your Knowledge: Quiz 582
Test Your Knowledge: Answers 582

24. Advanced Module Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583


Data Hiding in Modules 583
Minimizing from * Damage: _X and __all__ 584
Enabling Future Language Features 584
Mixed Usage Modes: __name__ and __main__ 585
Unit Tests with __name__ 586
Using Command-Line Arguments with __name__ 587
Changing the Module Search Path 590
The as Extension for import and from 591

Table of Contents | xix


Modules Are Objects: Metaprograms 591
Importing Modules by Name String 594
Transitive Module Reloads 595
Module Design Concepts 598
Module Gotchas 599
Statement Order Matters in Top-Level Code 599
from Copies Names but Doesn’t Link 600
from * Can Obscure the Meaning of Variables 601
reload May Not Impact from Imports 601
reload, from, and Interactive Testing 602
Recursive from Imports May Not Work 603
Chapter Summary 604
Test Your Knowledge: Quiz 604
Test Your Knowledge: Answers 605
Test Your Knowledge: Part V Exercises 605

Part VI. Classes and OOP

25. OOP: The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611


Why Use Classes? 612
OOP from 30,000 Feet 613
Attribute Inheritance Search 613
Classes and Instances 615
Class Method Calls 616
Coding Class Trees 616
OOP Is About Code Reuse 619
Chapter Summary 622
Test Your Knowledge: Quiz 622
Test Your Knowledge: Answers 622

26. Class Coding Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625


Classes Generate Multiple Instance Objects 625
Class Objects Provide Default Behavior 626
Instance Objects Are Concrete Items 626
A First Example 627
Classes Are Customized by Inheritance 629
A Second Example 630
Classes Are Attributes in Modules 631
Classes Can Intercept Python Operators 633
A Third Example 634
Why Use Operator Overloading? 636
The World’s Simplest Python Class 636

xx | Table of Contents
Classes Versus Dictionaries 639
Chapter Summary 641
Test Your Knowledge: Quiz 641
Test Your Knowledge: Answers 641

27. A More Realistic Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643


Step 1: Making Instances 644
Coding Constructors 644
Testing As You Go 645
Using Code Two Ways 646
Step 2: Adding Behavior Methods 648
Coding Methods 649
Step 3: Operator Overloading 651
Providing Print Displays 652
Step 4: Customizing Behavior by Subclassing 653
Coding Subclasses 653
Augmenting Methods: The Bad Way 654
Augmenting Methods: The Good Way 654
Polymorphism in Action 656
Inherit, Customize, and Extend 657
OOP: The Big Idea 658
Step 5: Customizing Constructors, Too 658
OOP Is Simpler Than You May Think 660
Other Ways to Combine Classes 660
Step 6: Using Introspection Tools 663
Special Class Attributes 664
A Generic Display Tool 665
Instance Versus Class Attributes 666
Name Considerations in Tool Classes 667
Our Classes’ Final Form 668
Step 7 (Final): Storing Objects in a Database 669
Pickles and Shelves 670
Storing Objects on a Shelve Database 671
Exploring Shelves Interactively 672
Updating Objects on a Shelve 674
Future Directions 675
Chapter Summary 677
Test Your Knowledge: Quiz 677
Test Your Knowledge: Answers 678

28. Class Coding Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681


The class Statement 681
General Form 681

Table of Contents | xxi


Example 682
Methods 684
Method Example 685
Calling Superclass Constructors 686
Other Method Call Possibilities 686
Inheritance 687
Attribute Tree Construction 687
Specializing Inherited Methods 687
Class Interface Techniques 689
Abstract Superclasses 690
Python 2.6 and 3.0 Abstract Superclasses 692
Namespaces: The Whole Story 693
Simple Names: Global Unless Assigned 693
Attribute Names: Object Namespaces 693
The “Zen” of Python Namespaces: Assignments Classify Names 694
Namespace Dictionaries 696
Namespace Links 699
Documentation Strings Revisited 701
Classes Versus Modules 703
Chapter Summary 703
Test Your Knowledge: Quiz 703
Test Your Knowledge: Answers 704

29. Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705


The Basics 705
Constructors and Expressions: __init__ and __sub__ 706
Common Operator Overloading Methods 706
Indexing and Slicing: __getitem__ and __setitem__ 708
Intercepting Slices 708
Index Iteration: __getitem__ 710
Iterator Objects: __iter__ and __next__ 711
User-Defined Iterators 712
Multiple Iterators on One Object 714
Membership: __contains__, __iter__, and __getitem__ 716
Attribute Reference: __getattr__ and __setattr__ 718
Other Attribute Management Tools 719
Emulating Privacy for Instance Attributes: Part 1 720
String Representation: __repr__ and __str__ 721
Right-Side and In-Place Addition: __radd__ and __iadd__ 723
In-Place Addition 725
Call Expressions: __call__ 725
Function Interfaces and Callback-Based Code 727
Comparisons: __lt__, __gt__, and Others 728

xxii | Table of Contents


The 2.6 __cmp__ Method (Removed in 3.0) 729
Boolean Tests: __bool__ and __len__ 730
Object Destruction: __del__ 732
Chapter Summary 733
Test Your Knowledge: Quiz 734
Test Your Knowledge: Answers 734

30. Designing with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737


Python and OOP 737
Overloading by Call Signatures (or Not) 738
OOP and Inheritance: “Is-a” Relationships 739
OOP and Composition: “Has-a” Relationships 740
Stream Processors Revisited 742
OOP and Delegation: “Wrapper” Objects 745
Pseudoprivate Class Attributes 747
Name Mangling Overview 748
Why Use Pseudoprivate Attributes? 748
Methods Are Objects: Bound or Unbound 750
Unbound Methods are Functions in 3.0 752
Bound Methods and Other Callable Objects 754
Multiple Inheritance: “Mix-in” Classes 756
Coding Mix-in Display Classes 757
Classes Are Objects: Generic Object Factories 768
Why Factories? 769
Other Design-Related Topics 770
Chapter Summary 770
Test Your Knowledge: Quiz 770
Test Your Knowledge: Answers 771

31. Advanced Class Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773


Extending Built-in Types 773
Extending Types by Embedding 774
Extending Types by Subclassing 775
The “New-Style” Class Model 777
New-Style Class Changes 778
Type Model Changes 779
Diamond Inheritance Change 783
New-Style Class Extensions 788
Instance Slots 788
Class Properties 792
__getattribute__ and Descriptors 794
Metaclasses 794
Static and Class Methods 795

Table of Contents | xxiii


Random documents with unrelated
content Scribd suggests to you:
Denise, very much astounded at this newly-developed piety of
Toni’s, did as she was bid, having been piously brought up. At the
end of a few minutes she rose, but Toni was obstinate. He wanted to
stay in the church all night on his knees. Denise, determined to find
out what ailed him, spoke to him with that tone of gentle authority
which he had never resisted since they were little children together,
walking hand in hand at Bienville. She dragged Toni out of the
church, stumbling along in the darkness, and he shut the door
carefully. They were only a step or two from their lodgings, and
climbing up to their two little rooms, Toni took Denise in his arms
and poured out the whole story of Nicolas and Pierre, sobbing
between times, and laughing, like one possessed. Denise wept—she
saw nothing to laugh at—and actually expressed some pity for the
two lost souls of Nicolas and Pierre. This seemed really impious to
Toni.
The recital did not take long, and then Toni, taking his cap, said:
“I must run now, as fast as I can, to the Château Bernard.
Monsieur Paul must know this.”
Denise did not detain him and he ran softly down stairs and took
his way through the dark streets and along the deserted highway
until he reached the park of the Château Bernard. He climbed the
wall and walked swiftly through the park until he got to the château,
standing white and stately upon its broad terraces. It was then quite
one o’clock in the morning. The sky had cleared and a great
hobgoblin moon was looking down on the church steeples of the
town, visible afar off. Toni knew the window of Paul’s room. It was
on the first floor above the ground floor, and at a corner. He knew
the only way to awaken Paul, without alarming the house, was to
throw pebbles at his window, but there were no pebbles to be
found. He remembered, however, that Paul was a light sleeper, and
going under the window Toni called out softly a dozen times—“Paul
—Paul—Monsieur.” Presently the window of the room came open,
and he heard Paul’s voice asking softly:
“Who is that?”
“It is I,” whispered Toni, creeping under the window. “Come
down.”
In a few moments a small door under the window opened
noiselessly, and Paul came out in his trousers and shirt. Toni caught
him around the neck and whispered in his ear:
“They are dead, Paul, both of them. They were fighting on the
railway track when the Paris train came along. I saw them both quite
dead.”
Paul knew at once whom Toni meant. A great wave of gratitude
welled up in his heart. He did not, like Toni, drop on his face and
weep and fall into a paroxysm of piety, but he felt his release from
the sentence of death pronounced against them both, as much as
Toni did.
“Then we are saved, Toni, from that knife-thrust in the heart or
that blow on the side of the head,” said Paul quietly. “Thank God!”
“I have told Denise,” whispered Toni, “now you go, Paul, and tell
Madame.”
Just then a light shone in Lucie’s window. She passed into Paul’s
room, and going to the open window, her white figure leaned out.
“I am coming in now, dearest,” called Paul softly, stepping under
the window. “I have good news.”
In a little while Toni was plodding back through the park. He
meant to be a model husband, the best father that ever lived, if God
should give him children, the most worthy, blameless corporal in the
French army. He meant to give all his substance to the poor,
including Denise’s dowry, to go to church twice a day on week-days
and three times on Sundays, and to lead a life which would be a
perfect combination of the contemplative and the actively charitable.
All of the time that he could spare from his military duties, he meant
to give to prayer, and to make Denise pray with him. He intended to
fast and to make Denise fast, too. Not St. Elizabeth, queen of
Hungary, married to St. Louis, king of France, could have led the life
which Toni, in these first moments, promised that he and Denise
should lead. Never was there on earth so good a man as Toni meant
to be thereafter.
THE END
A MASTERPIECE OF
FICTION.
The Guarded Flame.
By W. B. Maxwell, Author of
“Vivien.” Cloth, $1.50.
“‘The Guarded Flame’, by W. B.
Maxwell, is a book to challenge
the attention of the reading public
as a remarkable study of moral
law and its infraction. Mr. Maxwell
is the son of Miss M. E. Braddon
(Mrs. John Maxwell), whose
novels were famous a generation
ago, and his first book ‘Vivien’
made the English critics herald
him as a new force in the world of
letters. ‘The Guarded Flame’ is an
even more astonishing
production, a big book that takes
rank with the most important
fiction of the year. It is not a book
for those who read to be amused
or to be entertained. It touches
the deepest issues of life and
death.”—Albany Argus.
“The most powerfully written
book of the year.”—The
Independent.
“‘The Guarded Flame’ is
receiving high praise from the
critics everywhere.”—Chicago
Record-Herald.
“This is a book which cannot
fail to make its mark.”—Detroit
News.
“Great novels are few and the
appearance of one at any period
must give the early reviewer a
thrill of discovery. Such a one has
come unheralded; but from a
source whence it might have been
confidently expected. The author
is W. B. Maxwell, son of the
voluminous novelist known to the
world as Miss Braddon. His novel
is entitled ‘The Guarded Flame.’”—
Philadelphia Press.
“The books of W. B. Maxwell
are essentially for thinkers.”—St.
Louis Post-Dispatch.

D. APPLETON AND COMPANY,


NEW YORK.
A ROMANCE OF THE
CIVIL WAR.
The Victory.
By Molly Elliott Seawell, author
of “The Château of Montplaisir,”
“The Sprightly Romance of
Marsac,” etc. Illustrated. Cloth,
$1.50.
“With so delicate a touch and
appreciation of the detail of
domestic and plantation life, with
so wise comprehension of the
exalted and sometimes stilted
notions of Southern honor and
with humorous depiction of
African fidelity and bombast to
interest and amuse him, it only
gradually dawns on a reader that
‘The Victory’ is the truest and
most tragic presentation yet
before us of the rending of home
ties, the awful passions, the
wounded affections personal and
national, and the overwhelming
questions of honor which weighed
down a people in the war of son
against father and brother against
brother.”—Hartford Courant.
“Among the many romances
written recently about the Civil
War, this one by Miss Seawell
takes a high place.... Altogether,
‘The Victory,’ a title significant in
several ways, makes a strong
appeal to the lover of a good
tale.”—The Outlook.
“Miss Seawell’s narrative is not
only infused with a tender and
sympathetic spirit of romance and
surcharged with human interests,
but discloses, in addition, careful
and minute study of local
conditions and characteristic
mannerisms. It is an intimate
study of life on a Virginia
plantation during an emergent
and critical period of American
history.”—Philadelphia North
American.
“It is one of the romances that
make, by spirit as well as letter,
for youth and high feeling. It
embodies, perhaps, the best work
this author yet has done.”—
Chicago Record-Herald.
“Aside from the engaging story
itself and the excellent manner in
which it is told there is much of
historic interest in this vivid word-
picture of the customs and
manners of a period which has
formed the background of much
fiction.”—Brooklyn Citizen.

D. APPLETON AND COMPANY,


NEW YORK.
BY H. B. MARRIOTT
WATSON.
A Midsummer Day’s
Dream.
Ornamental Cloth, $1.50.
“Since Harland’s ‘The
Cardinal’s Snuff Box’ there has
been nothing to equal its clever
and graceful merriment.”—New
York Times.
“A delightful bit of romantic
foolery.”—New York Evening Post.
“A very beautiful story, in
which Mr. Watson has employed
his gifts in the employment of
language and the telling of a tale
to excellent advantage.”—St. Louis
Globe-Democrat.
“The little tale is graceful to a
degree, witty past expectation,
and pervaded with an illusive
spirit of poetry. It is nonsense ‘but
quite precious nonsense,’ as
Bunthorne used to say.”—Chicago
Tribune.

Twisted Eglantine.
Illustrated. Ornamental Cloth,
$1.50.
This is the history of a love
affair of a famous English beau in
the time of George IV. The
heroine is a ravishingly beautiful
country girl, with whom the beau
becomes infatuated. By the aid of
her country lover she is rescued
from the wiles of the fashionable
dandy, and his schemes are
thwarted.
“The story is on a high plane;
it is a romance which is entirely
free from the melodramatic
quality; in a word, a novel that
will enhance the author’s
reputation as a writer of fiction of
far more than ordinary
attainments.”—Brooklyn Eagle.
“He has presented to us the
only living beau we have met this
many a year; and where so many
hundreds have failed, to say this
is the highest compliment we can
pay his book.”—The Outlook.

D. APPLETON AND COMPANY,


NEW YORK.
BY LLOYD OSBOURNE.

Three Speeds Forward.


Uniquely illustrated with full-
page illustrations, head and tail
pieces and many sketches by Karl
Anderson and H. D. Williams.
Ornamental Cloth, $1.00.
“‘Three Speeds Forward’ is an
amusing automobile story by
Lloyd Osbourne, in which the
ostensible teller of what happened
is the girl heroine. A little
runabout is the important factor in
the love romance. The book is
prettily bound and printed and is
illustrated.”—Toledo Blade.
“‘Three Speeds Forward,’ by
Lloyd Osbourne, is a very brief
and most agreeable novelette
dealing with modern society and
the chug-chug wagon.”—
Philadelphia Inquirer.
“The climax of this story is
original and most humorous. The
action is rapid and consistent with
the subject in hand. Altogether it
is a most enjoyable little volume,
well illustrated and attractively
bound.”—Milwaukee Sentinel.
“It is a bright and sprightly
little story, very strongly flavored
with gasoline, but quite readable.
It is attractively and
characteristically illustrated.”—
New York Times.

Wild Justice.
Illustrated. Ornamental Cloth,
$1.50.
“Lloyd Osbourne’s stories of
the South Sea Islands are second
only to Stevenson’s on the same
theme. ‘Wild Justice’ is a volume
of these short stories, beginning
with that strong and haunting
tale, ‘The Renegade.’ These are
stories which will bear reading
more than once. They have an
atmosphere that it is restful to
breathe, once in a while, to the
dwellers in cities and the toilers of
these Northern lands where life is
such a stern affair.”—Denver Post.
“Mr. Lloyd Osbourne’s nine
stories of the South Sea Islands
(‘Wild Justice’) are told with a
Kiplingesque vigor, and well
illustrate their title. All are
eminently readable—not
overweighted with tragedy, as is
the wont of tales that deal with
the remote regions of the
earth.”—New York Times.
“Mr. Osbourne in ‘Wild Justice’
has given us a series of stories
about the Samoan Islands and
their islanders and their white
invaders, visitors and conquerors
which are vivid with humor and
pathos.”—New York Herald.

D. APPLETON AND COMPANY,


NEW YORK.
TWO CHARMING
STORIES.

The Little King of


Angel’s Landing.
By Elmore Elliott Peake.
Illustrated. Cloth, $1.25.
This is a story of a plucky little
cripple of indomitable energy and
perseverance. How, boy-like, he
forms an ideal love for his school
teacher and wins a great voting
contest for her; how he patiently
saves his pennies to get himself
“fixed”; how his faithful dog is
killed and the shock it brings to
the frail little soul; how he
struggles onward, upward, and at
last comes into his birthright—all
these are incidents of a story the
kindly humor and infinite pathos
of which are deeply appealing.
“There are tears and smiles in
every chapter of ‘The Little King of
Angel’s Landing.’”—Denver Post.
“There is a mighty human
interest—a something that takes
hold of your heart and sometimes
hurts it a bit, but which presently
makes you correspondingly glad—
in ‘The Little King of Angel’s
Landing.’”—Cincinnati Times-Star.

The House of Hawley.


By Elmore Elliott Peake.
Ornamental Cloth, $1.50.
“‘The House of Hawley,’ by
Elmore Elliott Peake, is one of the
‘homiest’ stories we have met in a
long while.... Instead of calling so
often for the great American
novel, perhaps we should give
more attention to the many good
American novels, of which ‘The
House of Hawley’ is one,
containing faithful and interesting
portrayal of life in some one of
the many and diversified sections
of the country.”—New York Globe.
“There is not a dull page in the
whole book. It is well worth
reading.”—St. Louis Star.
“‘The House of Hawley’ is a
fresh, readable story by Elmore
Elliott Peake, the theme of which
is laid in the ‘Egypt’ of southern
Illinois. The title fits better than
usual, and the characters depicted
are real people. There is not a
single stick of dead timber among
the various men and women.”—
Chicago Record-Herald.
D. APPLETON AND COMPANY,
NEW YORK.
“J. S. OF DALE’S”
GREATEST NOVEL.

In Cure of Her Soul.


By Frederic Jesup Stimson (“J. S.
of Dale”), author of “First
Harvests,” “King Noanett,”
“Guerndale,” etc. Illustrated by A.
B. Wenzell. Cloth, $1.50.
One of the big novels of the
year—big in theme, big in
treatment—big in its perspective
of humanity—normal, sinning,
repentant people of the kind that
one meets in real life. Two young
society people have a sudden love
affair and marriage. Then works
out a strange story of two
temperaments widely diverse, two
lives wholly apart, yet holding
together to an end that can only
bring peace and happiness. It is
one of the most powerful
arguments against the divorce
court ever put into the form of
fiction.
“A novel which stands head
and shoulders above its current
fellows.”—Providence Journal.
“One of the most important
novels of the year.”—Springfield
Union.
“A valuable contribution to
current fiction.”—New York Sun.
“A novel with a powerful motif.
It presents a study of the social
whirl of Greater New York; of a
young Harvard graduate who
loves twice; of a young wife, who,
led apart from her mate by the
gay maelstrom of the select,
plunges into the estrangement
with a butterfly flutter until she is
abruptly halted and faced about;
of the doings and sayings that go
to make the book what it is—one
of the best of the season.”—
Brooklyn Citizen.

D. APPLETON AND COMPANY,


NEW YORK.
TRANSCRIBER’S NOTES:
Illustrations that occurred in mid-paragraph have been moved either up or down, to avoid
interrupting the flow for the reader.
Typesetter's misspelling of "Herman" has been corrected to "Hermann" on page 85.
The period on the second paragraph on page 144 has been corrected to a colon, to
punctuate the paragraph correctly.
The typesetter's error "in-instinct" on page 325 has been corrected to "instinct."
Typesetter's misspelling of "Lucy", on page 301, has been corrected to "Lucie".
The typesetter's repetition of "and and" has been corrected on page 46.
"Chateau" has been corrected to "Château" in three places, (on the title page, in the list
of the author's books, and in the advertisement for "The Victory",) to regularize spelling
in this ebook.
Typesetter's misspelling of "insiduous" has been corrected to "insidious", on page 180.
Numerous changes have been made to regularize hyphenation across this ebook:
On page 180, the word "good-will" has been changed to "good will";
The words "half-American" have been changed to "half American," on pages 169 and
181;
On page 169, the phrase "closely cropped" has been hyphenated;
The phrase "love-affair", on page 298, has been corrected to "love affair";
On page 183, the phrase "longed-for" has been corrected to "longed for";
"Grown-up" on page 70, has been corrected to "grown up";
On page 253, the phrase "matter of fact" has been changed to "matter-of-fact";
The phrase "newly married" has been hyphenated;
In two instances, (pages 324 and 326), the phrase "far off" has been hyphenated; to
match other usages in the book;
On page 123, "downstairs" has been corrected to "down stairs";
On page 301, "wedding-breakfast" has been corrected to "wedding breakfast".
*** END OF THE PROJECT GUTENBERG EBOOK THE SECRET OF
TONI ***

Updated editions will replace the previous one—the old editions


will be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States
copyright in these works, so the Foundation (and you!) can copy
and distribute it in the United States without permission and
without paying copyright royalties. Special rules, set forth in the
General Terms of Use part of this license, apply to copying and
distributing Project Gutenberg™ electronic works to protect the
PROJECT GUTENBERG™ concept and trademark. Project
Gutenberg is a registered trademark, and may not be used if
you charge for an eBook, except by following the terms of the
trademark license, including paying royalties for use of the
Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such
as creation of derivative works, reports, performances and
research. Project Gutenberg eBooks may be modified and
printed and given away—you may do practically ANYTHING in
the United States with eBooks not protected by U.S. copyright
law. Redistribution is subject to the trademark license, especially
commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the


free distribution of electronic works, by using or distributing this
work (or any other work associated in any way with the phrase
“Project Gutenberg”), you agree to comply with all the terms of
the Full Project Gutenberg™ License available with this file or
online at www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand,
agree to and accept all the terms of this license and intellectual
property (trademark/copyright) agreement. If you do not agree
to abide by all the terms of this agreement, you must cease
using and return or destroy all copies of Project Gutenberg™
electronic works in your possession. If you paid a fee for
obtaining a copy of or access to a Project Gutenberg™
electronic work and you do not agree to be bound by the terms
of this agreement, you may obtain a refund from the person or
entity to whom you paid the fee as set forth in paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only


be used on or associated in any way with an electronic work by
people who agree to be bound by the terms of this agreement.
There are a few things that you can do with most Project
Gutenberg™ electronic works even without complying with the
full terms of this agreement. See paragraph 1.C below. There
are a lot of things you can do with Project Gutenberg™
electronic works if you follow the terms of this agreement and
help preserve free future access to Project Gutenberg™
electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright
law in the United States and you are located in the United
States, we do not claim a right to prevent you from copying,
distributing, performing, displaying or creating derivative works
based on the work as long as all references to Project
Gutenberg are removed. Of course, we hope that you will
support the Project Gutenberg™ mission of promoting free
access to electronic works by freely sharing Project Gutenberg™
works in compliance with the terms of this agreement for
keeping the Project Gutenberg™ name associated with the
work. You can easily comply with the terms of this agreement
by keeping this work in the same format with its attached full
Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.

1.E. Unless you have removed all references to Project


Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project
Gutenberg™ work (any work on which the phrase “Project
Gutenberg” appears, or with which the phrase “Project
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and
with almost no restrictions whatsoever. You may copy it,
give it away or re-use it under the terms of the Project
Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country
where you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of
the copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
containing a part of this work or any other work associated with
Project Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute


this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™
works unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or


providing access to or distributing Project Gutenberg™
electronic works provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project


Gutenberg™ electronic work or group of works on different
terms than are set forth in this agreement, you must obtain
permission in writing from the Project Gutenberg Literary
Archive Foundation, the manager of the Project Gutenberg™
trademark. Contact the Foundation as set forth in Section 3
below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright

You might also like