Python 3 Patterns, Recipes and Idioms Release 1.0
Total Page:16
File Type:pdf, Size:1020Kb
, Python 3 Patterns, Recipes and Idioms Release 1.0 Bruce Eckel September 27, 2017 Contents 1 Contributors 1 1.1 Thanks To................................................1 2 ToDo List 3 3 The remainder are from context, from the book.5 4 A Note To Readers 7 5 Introduction 9 5.1 A Team Effort..............................................9 5.2 Not an Introductory Book.......................................9 5.3 The License............................................... 10 5.4 The Printed Book............................................ 10 5.5 Translations............................................... 10 5.6 My Motives............................................... 11 6 Teaching Support 13 7 Book Development Rules 15 7.1 Contribute What You Can....................................... 15 7.2 Don’t Get Attached........................................... 15 7.3 Credit.................................................. 16 7.4 Mechanics................................................ 16 7.5 Diagrams................................................ 17 8 Developer Guide 19 8.1 Getting Started: The Easiest Approach............................... 19 8.2 For Windows Users.......................................... 19 8.3 Installing Sphinx............................................ 20 8.4 Getting the Development Branch of the Book............................ 20 8.5 Building the Book........................................... 21 8.6 Building the PDF............................................ 21 8.7 Setting up Mercurial.......................................... 21 8.8 Working with BitBucket and Mercurial............................... 22 8.9 A Simple Overview Of Editing and Merging............................ 23 8.10 Emacs for Editing Restructured Text................................. 23 i 9 Part I: Foundations 25 10 Python for Programmers 27 10.1 Scripting vs. Programming...................................... 27 10.2 Built-In Containers........................................... 28 10.3 Functions................................................ 29 10.4 Strings.................................................. 29 10.5 Classes.................................................. 30 10.6 Useful Techniques........................................... 33 10.7 Further Reading............................................ 33 11 Initialization and Cleanup 35 11.1 Initialization.............................................. 35 11.2 Cleanup................................................. 36 11.3 Further Reading............................................ 37 12 Unit Testing & Test-Driven Development 39 12.1 Write Tests First............................................. 40 12.2 Simple Python Testing......................................... 40 12.3 A Very Simple Framework...................................... 41 12.4 Writing Tests.............................................. 42 12.5 White-Box & Black-Box Tests..................................... 44 12.6 Running tests.............................................. 45 12.7 Automatically Executing Tests.................................... 47 12.8 Exercises................................................. 47 13 Python 3 Language Changes 49 14 Decorators 51 14.1 Decorators vs. the Decorator Pattern................................. 51 14.2 History of Macros........................................... 51 14.3 The Goal of Macros........................................... 52 14.4 What Can You Do With Decorators?................................. 52 14.5 Function Decorators.......................................... 52 14.6 Slightly More Useful.......................................... 54 14.7 Using Functions as Decorators.................................... 54 14.8 Review: Decorators without Arguments.............................. 55 14.9 Decorators with Arguments...................................... 56 14.10 Decorator Functions with Decorator Arguments.......................... 58 14.11 Further Reading............................................ 59 15 Metaprogramming 61 15.1 Basic Metaprogramming....................................... 62 15.2 The Metaclass Hook.......................................... 64 15.3 Example: Self-Registration of Subclasses.............................. 66 15.4 Example: Making a Class “Final”.................................. 67 15.5 Using __init__ vs. __new__ in Metaclasses........................... 68 15.6 Class Methods and Metamethods.................................. 70 15.7 The __prepare__() Metamethod................................. 72 15.8 Module-level __metaclass__ Assignment............................ 73 15.9 Metaclass Conflicts........................................... 73 15.10 Further Reading............................................ 73 16 Generators, Iterators, and Itertools 75 ii 17 Comprehensions 77 17.1 List Comprehensions......................................... 77 17.2 Nested Comprehensions....................................... 78 17.3 Techniques............................................... 79 17.4 A More Complex Example...................................... 80 17.5 Set Comprehensions.......................................... 83 17.6 Dictionary Comprehensions..................................... 83 18 Coroutines, Concurrency & Distributed Systems 85 18.1 The GIL................................................. 85 18.2 Multiprocessing............................................ 86 18.3 Further Reading............................................ 87 19 Jython 89 19.1 Installation............................................... 90 19.2 Scripting................................................. 91 19.3 Interpreter Motivation......................................... 92 19.4 Using Java libraries........................................... 94 19.5 Controlling Java from Jython..................................... 96 19.6 Controlling the Interpreter...................................... 99 19.7 Creating Java classes with Jython.................................. 106 19.8 Summary................................................ 110 19.9 Exercises................................................. 110 20 Part II: Idioms 111 21 Discovering the Details About Your Platform 113 22 A Canonical Form for Command-Line Programs 115 23 Messenger/Data Transfer Object 117 24 Part III: Patterns 119 25 The Pattern Concept 121 25.1 What is a Pattern?........................................... 121 25.2 Classifying Patterns.......................................... 122 25.3 Pattern Taxonomy........................................... 123 25.4 Design Structures............................................ 123 25.5 Design Principles............................................ 124 25.6 Further Reading............................................ 125 26 The Singleton 127 26.1 Exercises................................................. 131 27 Building Application Frameworks 133 27.1 Template Method............................................ 133 27.2 Exercises................................................. 134 28 Fronting for an Implementation 135 28.1 Proxy................................................... 136 28.2 State................................................... 137 29 StateMachine 139 29.1 Table-Driven State Machine...................................... 144 29.2 Tools................................................... 151 iii 29.3 Exercises................................................. 151 30 Decorator: Dynamic Type Selection 153 30.1 Basic Decorator Structure....................................... 154 30.2 A Coffee Example........................................... 154 30.3 Class for Each Combination...................................... 154 30.4 The Decorator Approach....................................... 156 30.5 Compromise.............................................. 158 30.6 Other Considerations......................................... 160 30.7 Further Reading............................................ 161 30.8 Exercises................................................. 161 31 Iterators: Decoupling Algorithms from Containers 163 31.1 Type-Safe Iterators........................................... 163 32 Factory: Encapsulating Object Creation 165 32.1 Simple Factory Method........................................ 165 32.2 Polymorphic Factories......................................... 167 32.3 Abstract Factories........................................... 169 32.4 Exercises................................................. 171 33 Function Objects 173 33.1 Command: Choosing the Operation at Runtime.......................... 173 33.2 Strategy: Choosing the Algorithm at Runtime........................... 174 33.3 Chain of Responsibility........................................ 175 33.4 Exercises................................................. 178 34 Changing the Interface 179 34.1 Adapter................................................. 179 34.2 Façade.................................................. 180 34.3 Exercises................................................. 181 35 Table-Driven Code: Configuration Flexibility 183 35.1 Table-Driven Code Using Anonymous Inner Classes....................... 183 36 Observer 185 36.1 Observing Flowers........................................... 186 37 Multiple Dispatching 197 38 Visitor 201 38.1 Exercises................................................. 202 39 Pattern Refactoring 205 39.1 Simulating the Trash Recycler.................................... 205 39.2 Improving the Design......................................... 208 39.3 A Pattern for Prototyping