所需积分/C币:9 2015-08-03 18:05:02 17.53MB PDF
收藏 收藏

Python’s simplicity lets you become productive quickly, but this often means you aren’t using everything it has to offer. With this hands-on guide, you’ll learn how to write effective, idiomatic Python code by leveraging its best—and possibly most neglected—features. Author Luciano Ramalho takes you through Python’s core language features and libraries, and shows you how to make your code shorter, faster, and more readable at the same time. Many experienced programmers try to bend Python to fit patterns they learned from other languages, and never discover Python features outside of their experience. With this book, those Python programmers will thoroughly learn how to become proficient in Python 3. This book covers: Python data model: understand how special methods are the key to the consistent behavior of objects Data structures: take full advantage of built-in types, and understand the text vs bytes duality in the Unicode age Functions as objects: view Python functions as first-class objects, and understand how this affects popular design patterns Object-oriented idioms: build classes by learning about references, mutability, interfaces, operator overloading, and multiple inheritance Control flow: leverage context managers, generators, coroutines, and concurrency with the concurrent.futures and asyncio packages Metaprogramming: understand how properties, attribute descriptors, class decorators, and metaclasses work Table of Contents Part I. Prologue Chapter 1. The Python Data Model Part II. Data structures Chapter 2. An array of sequences Chapter 3. Dictionaries and sets Chapter 4. Text versus bytes Part III. Functions as objects Chapter 5. First-class functions Chapter 6. Design patterns with first-class functions Chapter 7. Function decorators and closures Part IV. Object Oriented Idioms Chapter 8. Object references, mutability and recycling Chapter 9. A Pythonic object Chapter 10. Sequence hacking, hashing and slicing Chapter 11. Interfaces: from protoco
Fluent Python by luciano ramalho Copyright C 2014 Luciano Ramalho. All rights reserved Printed in the united states of america Published by o reilly 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 institutionalsalesdepartment800-998-9938orcorporate@oreilly.com Editors: Meghan Blanchette and Rachel roumeliotis Indexer: FIX ME Production editor: FIX me! Cover Designer: Karen Montgomery 〔 opyeditor: FIX ME! Interior Designer: David Futato Proofreader FIX me! llustrator: Rebecca demarest March 2015 First edition Revision History for the First Edition 2014-09-30: Early release revision 1 2014-12-05: Early release revision 2 2014-12-18: Early release revision 3 2015-01-27: Early release revision 4 2015-02-27: Early release revision 5 2015-04-15: Early release revision 6 2015-04-21: Early release revision 7 Seehttp://oreilly.com/catalog/errata.csp?isbn=9781491946008forreleasedetails Nutshell Handbook, the Nutshell Handbook logo, and the O Reilly logo are registered trademarks ofOReilly Media, Inc. FILL THIS IN! 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 forerrors or omissions, or for damages resulting from the use of the information contained herein 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 11 Boolean value of a custom typ 12 Overview of special methods 12 Why len is not a method Chapter summary 14 Further reading 15 Part. data structures 2. An array of sequences 19 Overview of built-in sequences 2 List comprehensions and generator expressions 21 List comprehensions and readabilit ity 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 Nested tuple unpacking 29 Named tuple Tuples as immutable lists 32 Slicing Why slices and range exclude the last item 33 Slice objects 34 Multi-dimensional slicing and ellipsis Assigning to slices 36 Using+ and with sequences 36 Building lists of lists 37 Augmented assignment with sequences 38 A += assignment puzzler list sort and the sorted built-in function 42 Managing ordered sequences with bisect Searching with bisect 44 Inserting with bisect insort When a list is not the answer arrays 48 Memo ry vIews 51 NumPy and scip 52 Deques and other queues 54 Chapter summary 57 Further reading 58 3. Dictionaries and sets......................63 Generic mapping types dict comprehensions 66 Overview of common mapping methods 66 Handling missing keys with setdefault 68 Mappings with flexible key lookup defaultdict: another take on missing keys 71 The missing method 72 Variations of dict 75 Subclassing userDict 76 Immutable mappings Set theory 79 set literals set comprehensions 81 Set operations 82 dict and set under the hood 85 A performance experiment 85 Hash tables in dictionaries 87 Table of contents Practical consequences of how dict works How sets work- practical consequences 93 Chapter summar 93 Further reading 94 4. Text versus bytes ,,97 Character issues 98 Byte essentials 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 107 How to discover the encoding of a byte sequence 108 BOM: a useful gremlin Handling text files 110 Encoding defaults: a madhouse 113 Normalizing Unicode for saner comparisons 116 Case foldin Utility functions for normalized text matching 120 Extreme normalization: taking out diacritics 121 Sortin g Unicode text 124 Sorting with the Unicode Collation Algorithm 126 The Unicode database 126 Dual mode str and bytes aPis 128 str versus bytes in regular expressions 129 str versus bytes on os functions 130 Chapter summar 132 Further reading g 133 Part l. 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 User defined callable types 145 Function introspection 147 Table of contentsⅶi 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 readin 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 non local declaration 195 Implementing a simple decorator 197 How it works 198 Decorators in the standard library 200 Memoization with functools. lru cache 200 Generic functions with single dispatch 202 Stacked decorators 205 Parametrized decorators 206 A parametrized registration decorator 206 The parametrized clock decorator 209 Chapter summary 211 Further reading 212 I Table of Contents Part Iv. Object Oriented Idioms 8. Object references, mutability and recycling 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 227 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 immutable 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 vector 2d 257 Private and“ protected” attributes in Python 263 Saving space with the slots class attribute 265 The problems with slots 267 Overriding class attributes 268 Chapter summary 270 Further reading 271 10. Sequence hacking, hashing and Vector: a user-defined sequence type 278 Vector take#1: Vector 2d compatible 278 Protocols and duck typing 281 Vector take # 2: a sliceable sequence 282 How slicing works 283 A slice-aware getitem 285 Table of contents

试读 127P Fluent.Python.1491946008
限时抽奖 低至0.43元/次
身份认证后 购VIP低至7折
小小洗白白 写python必备工具书,下载下来有时间就翻翻
StuDog 可以可以,这正是我需要的
Eddiechiu 不错不错,读起来有点难。
tailorstone 很好的书,感谢分享!
js3707 虽然不是final的,但是也非常不错,谢谢啦
csc57 最棒的书有关python
fireboy100 很不错的书,值得推荐。
dianeylee 非正式发行版,但可以看看。
u011123416 资源很好,非扫描版本。不过是非正式发布的版本。现已有正式版。
alias_888 不错,不过现在有更新的版本了。感谢!!
  • 至尊王者

关注 私信
Fluent.Python.1491946008 9积分/C币 立即下载

试读结束, 可继续阅读

9积分/C币 立即下载