基本信息
源码名称:Luciano Ramalho - Fluent Python_ Clear, Concise, and Effective Programming (2015, O’Reilly Media).pdf
源码大小:16.93M
文件格式:.pdf
开发语言:Python
更新时间:2020-09-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

经典书籍 fluent python

Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Part I. Prologue
1. The Python Data Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Pythonic Card Deck 4
How Special Methods Are Used 8
Emulating Numeric Types 9
String Representation 11
Arithmetic Operators 12
Boolean Value of a Custom Type 12
Overview of Special Methods 13
Why len Is Not a Method 14
Chapter Summary 14
Further Reading 15
Part II. Data Structures
2. An Array of Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Overview of Built-In Sequences 20
List Comprehensions and Generator Expressions 21
List Comprehensions and Readability 21
Listcomps Versus map and filter 23
Cartesian Products 23
Generator Expressions 25
Tuples Are Not Just Immutable Lists 26
Tuples as Records 26
Tuple Unpacking 27
v
Nested Tuple Unpacking 29
Named Tuples 30
Tuples as Immutable Lists 32
Slicing 33
Why Slices and Range Exclude the Last Item 33
Slice Objects 34
Multidimensional Slicing and Ellipsis 35
Assigning to Slices 36
Using   and * with Sequences 36
Building Lists of Lists 37
Augmented Assignment with Sequences 38
A  = Assignment Puzzler 40
list.sort and the sorted Built-In Function 42
Managing Ordered Sequences with bisect 44
Searching with bisect 44
Inserting with bisect.insort 47
When a List Is Not the Answer 48
Arrays 48
Memory Views 51
NumPy and SciPy 52
Deques and Other Queues 55
Chapter Summary 57
Further Reading 59
3. Dictionaries and Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Generic Mapping Types 64
dict Comprehensions 66
Overview of Common Mapping Methods 66
Handling Missing Keys with setdefault 68
Mappings with Flexible Key Lookup 70
defaultdict: Another Take on Missing Keys 70
The __missing__ Method 72
Variations of dict 75
Subclassing UserDict 76
Immutable Mappings 77
Set Theory 79
set Literals 80
Set Comprehensions 81
Set Operations 82
dict and set Under the Hood 85
A Performance Experiment 85
Hash Tables in Dictionaries 87
vi | Table of Contents
Practical Consequences of How dict Works 90
How Sets Work—Practical Consequences 93
Chapter Summary 93
Further Reading 94
4. Text versus Bytes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Character Issues 98
Byte Essentials 99
Structs and Memory Views 102
Basic Encoders/Decoders 103
Understanding Encode/Decode Problems 105
Coping with UnicodeEncodeError 105
Coping with UnicodeDecodeError 106
SyntaxError When Loading Modules with Unexpected Encoding 108
How to Discover the Encoding of a Byte Sequence 109
BOM: A Useful Gremlin 110
Handling Text Files 111
Encoding Defaults: A Madhouse 114
Normalizing Unicode for Saner Comparisons 117
Case Folding 119
Utility Functions for Normalized Text Matching 120
Extreme “Normalization”: Taking Out Diacritics 121
Sorting Unicode Text 124
Sorting with the Unicode Collation Algorithm 126
The Unicode Database 127
Dual-Mode str and bytes APIs 129
str Versus bytes in Regular Expressions 129
str Versus bytes on os Functions 130
Chapter Summary 132
Further Reading 133
Part III. Functions as Objects
5. First-Class Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Treating a Function Like an Object 140
Higher-Order Functions 141
Modern Replacements for map, filter, and reduce 142
Anonymous Functions 143
The Seven Flavors of Callable Objects 144
User-Defined Callable Types 145
Function Introspection 146
Table of Contents | vii
From Positional to Keyword-Only Parameters 148
Retrieving Information About Parameters 150
Function Annotations 154
Packages for Functional Programming 156
The operator Module 156
Freezing Arguments with functools.partial 159
Chapter Summary 161
Further Reading 162
6. Design Patterns with First-Class Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Case Study: Refactoring Strategy 168
Classic Strategy 168
Function-Oriented Strategy 172
Choosing the Best Strategy: Simple Approach 175
Finding Strategies in a Module 176
Command 177
Chapter Summary 179
Further Reading 180
7. Function Decorators and Closures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Decorators 101 184
When Python Executes Decorators 185
Decorator-Enhanced Strategy Pattern 187
Variable Scope Rules 189
Closures 192
The nonlocal Declaration 195
Implementing a Simple Decorator 196
How It Works 198
Decorators in the Standard Library 199
Memoization with functools.lru_cache 200
Generic Functions with Single Dispatch 202
Stacked Decorators 205
Parameterized Decorators 206
A Parameterized Registration Decorator 206
The Parameterized Clock Decorator 209
Chapter Summary 211
Further Reading 212
viii | Table of Contents
Part IV. Object-Oriented Idioms
8. Object References, Mutability, and Recycling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Variables Are Not Boxes 220
Identity, Equality, and Aliases 221
Choosing Between == and is 223
The Relative Immutability of Tuples 224
Copies Are Shallow by Default 225
Deep and Shallow Copies of Arbitrary Objects 228
Function Parameters as References 229
Mutable Types as Parameter Defaults: Bad Idea 230
Defensive Programming with Mutable Parameters 232
del and Garbage Collection 234
Weak References 236
The WeakValueDictionary Skit 237
Limitations of Weak References 239
Tricks Python Plays with Immutables 240
Chapter Summary 242
Further Reading 243
9. A Pythonic Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Object Representations 248
Vector Class Redux 248
An Alternative Constructor 251
classmethod Versus staticmethod 252
Formatted Displays 253
A Hashable Vector2d 257
Private and “Protected” Attributes in Python 262
Saving Space with the __slots__ Class Attribute 264
The Problems with __slots__ 267
Overriding Class Attributes 267
Chapter Summary 269
Further Reading 271
10. Sequence Hacking, Hashing, and Slicing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Vector: A User-Defined Sequence Type 276
Vector Take #1: Vector2d Compatible 276
Protocols and Duck Typing 279
Vector Take #2: A Sliceable Sequence 280
How Slicing Works 281
A Slice-Aware __getitem__ 283
Vector Take #3: Dynamic Attribute Access 284
Table of Contents | ix
Vector Take #4: Hashing and a Faster == 288
Vector Take #5: Formatting 294
Chapter Summary 301
Further Reading 302
11. Interfaces: From Protocols to ABCs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Interfaces and Protocols in Python Culture 308
Python Digs Sequences 310
Monkey-Patching to Implement a Protocol at Runtime 312
Alex Martelli’s Waterfowl 314
Subclassing an ABC 319
ABCs in the Standard Library 321
ABCs in collections.abc 321
The Numbers Tower of ABCs 323
Defining and Using an ABC 324
ABC Syntax Details 328
Subclassing the Tombola ABC 329
A Virtual Subclass of Tombola 332
How the Tombola Subclasses Were Tested 335
Usage of register in Practice 338
Geese Can Behave as Ducks 338
Chapter Summary 340
Further Reading 342
12. Inheritance: For Good or For Worse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Subclassing Built-In Types Is Tricky 348
Multiple Inheritance and Method Resolution Order 351
Multiple Inheritance in the Real World 356
Coping with Multiple Inheritance 358
1. Distinguish Interface Inheritance from Implementation Inheritance 359
2. Make Interfaces Explicit with ABCs 359
3. Use Mixins for Code Reuse 359
4. Make Mixins Explicit by Naming 359
5. An ABC May Also Be a Mixin; The Reverse Is Not True 360
6. Don’t Subclass from More Than One Concrete Class 360
7. Provide Aggregate Classes to Users 360
8. “Favor Object Composition Over Class Inheritance.” 361
Tkinter: The Good, the Bad, and the Ugly 361
A Modern Example: Mixins in Django Generic Views 362
Chapter Summary 366
Further Reading 367
x | Table of Contents
13. Operator Overloading: Doing It Right. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Operator Overloading 101 372
Unary Operators 372
Overloading   for Vector Addition 375
Overloading * for Scalar Multiplication 380
Rich Comparison Operators 384
Augmented Assignment Operators 388
Chapter Summary 392
Further Reading 393
Part V. Control Flow
14. Iterables, Iterators, and Generators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Sentence Take #1: A Sequence of Words 402
Why Sequences Are Iterable: The iter Function 404
Iterables Versus Iterators 405
Sentence Take #2: A Classic Iterator 409
Making Sentence an Iterator: Bad Idea 411
Sentence Take #3: A Generator Function 412
How a Generator Function Works 413
Sentence Take #4: A Lazy Implementation 416
Sentence Take #5: A Generator Expression 417
Generator Expressions: When to Use Them 419
Another Example: Arithmetic Progression Generator 420
Arithmetic Progression with itertools 423
Generator Functions in the Standard Library 424
New Syntax in Python 3.3: yield from 433
Iterable Reducing Functions 434
A Closer Look at the iter Function 436
Case Study: Generators in a Database Conversion Utility 437
Generators as Coroutines 439
Chapter Summary 439
Further Reading 440
15. Context Managers and else Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Do This, Then That: else Blocks Beyond if 448
Context Managers and with Blocks 450
The contextlib Utilities 454
Using @contextmanager 455
Chapter Summary 459
Further Reading 459
Table of Contents | xi
16. Coroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
How Coroutines Evolved from Generators 464
Basic Behavior of a Generator Used as a Coroutine 465
Example: Coroutine to Compute a Running Average 468
Decorators for Coroutine Priming 469
Coroutine Termination and Exception Handling 471
Returning a Value from a Coroutine 475
Using yield from 477
The Meaning of yield from 483
Use Case: Coroutines for Discrete Event Simulation 489
About Discrete Event Simulations 489
The Taxi Fleet Simulation 490
Chapter Summary 498
Further Reading 500
17. Concurrency with Futures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Example: Web Downloads in Three Styles 505
A Sequential Download Script 507
Downloading with concurrent.futures 509
Where Are the Futures? 511
Blocking I/O and the GIL 515
Launching Processes with concurrent.futures 515
Experimenting with Executor.map 517
Downloads with Progress Display and Error Handling 520
Error Handling in the flags2 Examples 525
Using futures.as_completed 527
Threading and Multiprocessing Alternatives 530
Chapter Summary 530
Further Reading 531
18. Concurrency with asyncio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Thread Versus Coroutine: A Comparison 539
asyncio.Future: Nonblocking by Design 545
Yielding from Futures, Tasks, and Coroutines 546
Downloading with asyncio and aiohttp 548
Running Circling Around Blocking Calls 552
Enhancing the asyncio downloader Script 554
Using asyncio.as_completed 555
Using an Executor to Avoid Blocking the Event Loop 560
From Callbacks to Futures and Coroutines 562
Doing Multiple Requests for Each Download 564
Writing asyncio Servers 567
xii | Table of Contents
An asyncio TCP Server 568
An aiohttp Web Server 573
Smarter Clients for Better Concurrency 576
Chapter Summary 577
Further Reading 579
Part VI. Metaprogramming
19. Dynamic Attributes and Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Data Wrangling with Dynamic Attributes 586
Exploring JSON-Like Data with Dynamic Attributes 588
The Invalid Attribute Name Problem 591
Flexible Object Creation with __new__ 592
Restructuring the OSCON Feed with shelve 594
Linked Record Retrieval with Properties 598
Using a Property for Attribute Validation 604
LineItem Take #1: Class for an Item in an Order 604
LineItem Take #2: A Validating Property 605
A Proper Look at Properties 606
Properties Override Instance Attributes 608
Property Documentation 610
Coding a Property Factory 611
Handling Attribute Deletion 614
Essential Attributes and Functions for Attribute Handling 616
Special Attributes that Affect Attribute Handling 616
Built-In Functions for Attribute Handling 616
Special Methods for Attribute Handling 617
Chapter Summary 619
Further Reading 619
20. Attribute Descriptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Descriptor Example: Attribute Validation 625
LineItem Take #3: A Simple Descriptor 626
LineItem Take #4: Automatic Storage Attribute Names 631
LineItem Take #5: A New Descriptor Type 637
Overriding Versus Nonoverriding Descriptors 640
Overriding Descriptor 642
Overriding Descriptor Without __get__ 643
Nonoverriding Descriptor 644
Overwriting a Descriptor in the Class 645
Methods Are Descriptors 646
Table of Contents | xiii
Descriptor Usage Tips 648
Descriptor docstring and Overriding Deletion 650
Chapter Summary 651
Further Reading 651
21. Class Metaprogramming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
A Class Factory 656
A Class Decorator for Customizing Descriptors 659
What Happens When: Import Time Versus Runtime 661
The Evaluation Time Exercises 662
Metaclasses 101 666
The Metaclass Evaluation Time Exercise 669
A Metaclass for Customizing Descriptors 673
The Metaclass __prepare__ Special Method 675
Classes as Objects 677
Chapter Summary 678
Further Reading 679
Afterword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
A. Support Scripts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Python Jargon. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725