【实例简介】
流利的Python英语原版高清带书签。这本书非常强大
Fluent Python
Luciano ramalho
Beijing. Boston Farnham. Sebastopol. Tokyo OREILLY
Fluent Python
by luciano ramalho
Copyright O 2015 Luciano Gama de Sousa Ramalho. All rights reserved
Printed in the United States of america
Published by Oreilly Media, InC, 1005 Gravenstein Highway North, Sebastopol, CA 95472
OReilly books may be purchased for educational,business, or sales promotional use. Online editions are
alsoavailableformosttitles(http://safaribooksonline.com).Formoreinformationcontactourcorporate
institutionalsalesdepartment:800-998-9938orcorporate@oreilly.com
Editors: Meghan Blanchette and Rachel Roumeliotis Indexer: Judy Mcconville
Production Editor: Melanie Yarbrough
Cover Designer: Ellie Volckhausen
〔 opyeditor: Kim Cofer
Interior Designer: David Futato
Proofreader: Jasmine Kwityn
lustrator: Rebecca demarest
August 2015
First edition
Revision history for the First Edition:
2015-07-24: First release
2015-08-21: Second release
http://oreilly.com/catalog/errata.csp?isbn=9781491946008forreleasedetails
The O Reilly logo is a registered trademark of O'Reilly Media, Inc. Fluent Python, the cover image, and
related trade dress are trademarks of o reilly media, Inc
While the publisher and author have used good faith efforts to ensure that the information and instructions
contained in this work are accurate the publisher and the author disclaim all responsibility for errors or
omissions, including without limitation responsibility for damages resulting from the use of or reliance on
this work. Use of the information and instructions contained in this work is at your own risk if any code
samples or other technology this work contains or describes is subject to open source licenses or the intel
lectual property rights of others, it is your responsibility to ensure that your use thereof complies with such
licenses and/or rights
ISBN:978-1-491-94600-8
Para marta, com todo o meu amor
Table of contents
Preface
Part I. Prologue
The python Data Model
。春鲁鲁。鲁春
A Pythonic Card Deck
How Special Methods Are Used
Emulating Numeric Types
String representation
Arithmetic Operators
12
Boolean value of a Custom Type
Overview of Special Methods
13
Why len is not a method
14
Chapter Summary
14
Further Reading
15
Part l. 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
Cartesian Products
23
Generator Expressions
25
Tuples Are Not Just Immutable Lists
Tuples as Records
26
Tuple unpacking
27
Nested Tuple Unpacking
29
d tuple
Tuples as Immutable Lists
32
Slicing
Why Slices and Range Exclude the Last Item
33
Slice objects
34
Multidimensional Slicing and Ellipsis
35
Assigning to Slices
36
sing+ 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
Searching with bisect
Inserting with bisect insort
When a list is not the answer
48
arrays
48
Memory views
51
Num Py and SciP
52
Deques and other queues
Chapter summary
57
Further Reading
59
3, Dictionaries and Sets
Generic Mapping Types
64
dict Comprehensions
66
Overview of Common Mapping Methods
Handling Missing Keys with setdefault
68
Mappings with Flexible Key lookup
70
defaultdict: Another Take on Missing Keys
The missing Method
Variations of dict
Subclassing UserDict
Immutable Mappings
Set theory
79
set literals
Set comprehensions
81
Set operations
82
dict and set Under the hood
A Performance Experiment
85
Hash tables in dictionaries
87
ⅵi| 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
Byte essential
Structs and memory views
102
Basic Encoders/decoders
103
Understanding Encode/ Decode Problems
105
Coping with UnicodeEncodeError
105
Coping with UnicodeDecodeerror
106
Syntax Error 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
g
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 Il. 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
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
159
Chapter summary
161
Further readins
162
6. Design patterns with First-Class functions
167
Case Study: Refactoring Strateg.
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 readins
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. Iru 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 readins
212
I Table of Contents
【实例截图】
【核心代码】