在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → Pro Python

Pro Python

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:4.28M
  • 下载次数:8
  • 浏览次数:59
  • 发布时间:2022-07-05
  • 实例类别:Python语言基础
  • 发 布 人:spy769
  • 文件格式:.pdf
  • 所需积分:2
 相关标签: python python Pro py

实例介绍

【实例简介】Pro Python

【实例截图】

【核心代码】

Contents
? ? Contents.................................................................................................................iv
? ? About the Author..................................................................................................xvi
? ? About the Technical Reviewer............................................................................xvii
? ? Acknowledgments.............................................................................................xviii
? ? Introduction.........................................................................................................xix
? ? Chapter 1: Principles and Philosophy.....................................................................1
The Zen of Python..............................................................................................................1
Beautiful Is Better Than Ugly....................................................................................................................2
Explicit Is Better Than Implicit..................................................................................................................2
Simple Is Better Than Complex ................................................................................................................3
Complex Is Better Than Complicated........................................................................................................3
Flat Is Better Than Nested........................................................................................................................4
Sparse Is Better Than Dense....................................................................................................................5
Readability Counts....................................................................................................................................5
Special Cases Aren’t Special Enough to Break the Rules.........................................................................6
Although Practicality Beats Purity............................................................................................................6
Errors Should Never Pass Silently............................................................................................................7
Unless Explicitly Silenced.........................................................................................................................8
In the Face of Ambiguity, Refuse the Temptation to Guess......................................................................9
There Should Be One—and Preferably Only One— Obvious Way to Do It.............................................10
Although That Way May Not Be Obvious at First Unless You’re Dutch...................................................10
Now Is Better Than Never.......................................................................................................................11
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
v
Although Never Is Often Better Than Right Now.....................................................................................11
If the Implementation is Hard to Explain, It’s a Bad Idea........................................................................11
If the Implementation is Easy to Explain, It May Be a Good Idea............................................................11
Namespaces Are One Honking Great Idea— Let’s Do More of Those!...................................................12
Don’t Repeat Yourself......................................................................................................12
Loose Coupling................................................................................................................13
The Samurai Principle......................................................................................................13
The Pareto Principle.........................................................................................................14
The Robustness Principle................................................................................................14
Backward Compatibility...................................................................................................15
The Road to Python 3.0....................................................................................................16
Taking It With You............................................................................................................17
? ? Chapter 2: Advanced Basics.................................................................................19
General Concepts.............................................................................................................19
Iteration..................................................................................................................................................19
Caching...................................................................................................................................................20
Transparency..........................................................................................................................................21
Control Flow.....................................................................................................................21
Catching Exceptions...............................................................................................................................21
Exception Chains....................................................................................................................................24
When Everything Goes Right ..................................................................................................................26
Proceeding Regardless of Exceptions....................................................................................................27
Optimizing Loops....................................................................................................................................29
The with Statement................................................................................................................................29
Conditional Expressions .........................................................................................................................31
Iteration ...........................................................................................................................33
Sequence Unpacking..............................................................................................................................34
List Comprehensions..............................................................................................................................35
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
vi
Generator Expressions............................................................................................................................36
Set Comprehensions...............................................................................................................................37
Dictionary Comprehensions....................................................................................................................37
Chaining Iterables Together ...................................................................................................................38
Zipping Iterables Together......................................................................................................................38
Collections.......................................................................................................................39
Sets ........................................................................................................................................................39
Named Tuples.........................................................................................................................................43
Ordered Dictionaries...............................................................................................................................44
Dictionaries with Defaults ......................................................................................................................44
Importing Code................................................................................................................45
Fallback Imports.....................................................................................................................................45
Importing from the Future......................................................................................................................46
Using __all__ to Customize Imports.......................................................................................................47
Relative Imports......................................................................................................................................48
The __import__() function......................................................................................................................49
The importlib module..............................................................................................................................51
Taking It With You............................................................................................................52
? ? Chapter 3: Functions.............................................................................................53
Arguments.......................................................................................................................53
Planning for Flexibility............................................................................................................................54
Variable Positional Arguments................................................................................................................54
Variable Keyword Arguments.................................................................................................................55
Combining Different Kinds of Arguments...............................................................................................56
Invoking Functions with Variable Arguments.........................................................................................59
Preloading Arguments............................................................................................................................60
Introspection...........................................................................................................................................61
Example: Identifying Argument Values...................................................................................................62
Example: A More Concise Version..........................................................................................................64
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
vii
Example: Validating Arguments..............................................................................................................66
Decorators.......................................................................................................................67
Closures..................................................................................................................................................69
Wrappers................................................................................................................................................71
Decorators with Arguments....................................................................................................................72
Decorators with—or without—Arguments............................................................................................74
Example: Memoization ...........................................................................................................................75
Example: A Decorator to Create Decorators...........................................................................................77
Function Annotations.......................................................................................................78
Example: Type Safety.............................................................................................................................79
Factoring Out the Boilerplate..................................................................................................................86
Example: Type Coercion.........................................................................................................................88
Annotating with Decorators....................................................................................................................90
Example: Type Safety as a Decorator.....................................................................................................90
Generators.......................................................................................................................94
Lambdas..........................................................................................................................96
Introspection....................................................................................................................97
Identifying Object Types.........................................................................................................................98
Modules and Packages...........................................................................................................................98
Docstrings ..............................................................................................................................................99
Taking It With You..........................................................................................................101
? ? Chapter 4: Classes..............................................................................................103
Inheritance.....................................................................................................................103
Multiple Inheritance..............................................................................................................................105
Method Resolution Order (MRO)...........................................................................................................106
Example: C3 Algorithm.........................................................................................................................109
Using super() to Pass Control to Other Classes....................................................................................115
Introspection.........................................................................................................................................117
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
viii
How Classes Are Created ..............................................................................................119
Creating Classes at Runtime ................................................................................................................120
Metaclasses..........................................................................................................................................121
Example: Plugin Framework.................................................................................................................122
Controlling the Namespace..................................................................................................................125
Attributes.......................................................................................................................126
Properties.............................................................................................................................................127
Descriptors...........................................................................................................................................129
Methods.........................................................................................................................131
Unbound Methods.................................................................................................................................131
Bound Methods.....................................................................................................................................132
Class Methods......................................................................................................................................133
Static Methods......................................................................................................................................134
Assigning Functions to Classes and Instances.....................................................................................135
Magic Methods..............................................................................................................135
Creating Instances................................................................................................................................136
Example: Automatic Subclasses...........................................................................................................137
Dealing with Attributes.........................................................................................................................138
String Representations.........................................................................................................................140
Taking It With You..........................................................................................................142
? ? Chapter 5: Common Protocols............................................................................143
Basic Operations............................................................................................................143
Mathematical Operations .....................................................................................................................144
Bitwise Operations ...............................................................................................................................148
Variations..............................................................................................................................................150
Numbers........................................................................................................................152
Sign Operations....................................................................................................................................154
Comparison Operations........................................................................................................................154
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
ix
Iterables.........................................................................................................................155
Example: Repeatable Generators .........................................................................................................158
Sequences.....................................................................................................................159
Mappings.......................................................................................................................164
Callables........................................................................................................................165
Context Managers..........................................................................................................166
Taking It With You..........................................................................................................168
? ? Chapter 6: Object Management ..........................................................................169
Namespace Dictionary...................................................................................................170
Example: Borg Pattern..........................................................................................................................170
Example: Self-caching properties ........................................................................................................173
Garbage Collection.........................................................................................................176
Reference Counting..............................................................................................................................177
Cyclical References..............................................................................................................................178
Weak References..................................................................................................................................180
Pickling..........................................................................................................................182
Copying..........................................................................................................................186
Shallow Copies.....................................................................................................................................187
Deep Copies..........................................................................................................................................188
Taking It With You..........................................................................................................190
? ? Chapter 7: Strings...............................................................................................191
Bytes..............................................................................................................................191
Simple Conversion: chr() and ord().......................................................................................................192
Complex Conversion: The Struct Module..............................................................................................193
Text................................................................................................................................195
Unicode.................................................................................................................................................196
Encodings.............................................................................................................................................196
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
x
Simple Substitution .......................................................................................................198
Formatting.....................................................................................................................201
Looking Up Values Within Objects........................................................................................................202
Distinguishing Types of Strings............................................................................................................202
Standard Format Specification.............................................................................................................203
Example: Plain Text Table of Contents.................................................................................................204
Custom Format Specification...............................................................................................................205
Taking It With You..........................................................................................................206
? ? Chapter 8: Documentation..................................................................................207
Proper Naming...............................................................................................................207
Comments......................................................................................................................208
Docstrings......................................................................................................................208
Describe What the Function Does ........................................................................................................209
Explain the Arguments .........................................................................................................................209
Don’t Forget the Return Value..............................................................................................................209
Include Any Expected Exceptions.........................................................................................................210
Documentation Outside the Code..................................................................................210
Installation and Configuration...............................................................................................................210
Tutorials................................................................................................................................................210
Reference Documents..........................................................................................................................210
Documentation Utilities .................................................................................................211
Formatting............................................................................................................................................212
Links.....................................................................................................................................................213
Sphinx...................................................................................................................................................214
Taking It With You..........................................................................................................215
? ? Chapter 9: Testing...............................................................................................217
Test-Driven Development (TDD) ....................................................................................217
Doctests.........................................................................................................................218
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
xi
Formatting Code...................................................................................................................................218
Representing Output.............................................................................................................................218
Integrating With Documentation...........................................................................................................219
Running Tests.......................................................................................................................................220
The unittest module.......................................................................................................221
Setting Up.............................................................................................................................................221
Writing Tests.........................................................................................................................................222
Other Comparisons...............................................................................................................................226
Testing Strings and Other Sequence Content.......................................................................................226
Testing Exceptions ...............................................................................................................................227
Testing Identity.....................................................................................................................................229
Tearing Down .......................................................................................................................................229
Providing a Custom Test Class......................................................................................230
Changing Test Behavior........................................................................................................................230
Taking It With You..........................................................................................................231
? ? Chapter 10: Distribution .....................................................................................233
Licensing .......................................................................................................................233
GNU General Public License (GPL)........................................................................................................233
Affero General Public License (AGPL)...................................................................................................234
GNU Lesser General Public License (LGPL)...........................................................................................235
Berkeley Software Distribution (BSD) License......................................................................................235
Other Licenses......................................................................................................................................236
Packaging......................................................................................................................236
setup.py................................................................................................................................................237
MANIFEST.in.........................................................................................................................................239
The sdist command..............................................................................................................................240
Distribution....................................................................................................................241
Taking It With You..........................................................................................................242
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
xii
? ? Chapter 11: Sheets: A CSV Framework...............................................................243
Building a Declarative Framework.................................................................................244
Introducing Declarative Programming..................................................................................................244
To Build or Not to Build?.......................................................................................................................245
Building the Framework................................................................................................246
Managing Options.................................................................................................................................247
Defining Fields......................................................................................................................................249
Attaching a Field to a Class..................................................................................................................250
Adding a Metaclass..............................................................................................................................252
Bringing It Together..............................................................................................................................255
Ordering Fields ..............................................................................................................256
DeclarativeMeta.__prepare__()............................................................................................................256
Column.__init__().................................................................................................................................258
Column.__new__()...............................................................................................................................262
CounterMeta.__call__()........................................................................................................................263
Choosing an Option...............................................................................................................................264
Building a Field Library..................................................................................................264
StringField............................................................................................................................................265
IntegerColumn......................................................................................................................................266
FloatColumn..........................................................................................................................................266
DecimalColumn ....................................................................................................................................266
DateColumn..........................................................................................................................................267
Getting Back to CSV.......................................................................................................271
Checking Arguments............................................................................................................................272
Populating Values.................................................................................................................................274
The Reader...........................................................................................................................................276
The Writer.............................................................................................................................................280
Taking It With You..........................................................................................................282
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
xiii
? ? PEP 8: Style Guide for Python.............................................................................283
Introduction ...................................................................................................................283
A Foolish Consistency is the Hobgoblin of Little Minds.................................................283
Code Layout...................................................................................................................284
Indentation............................................................................................................................................284
Tabs or Spaces?...................................................................................................................................284
Maximum Line Length..........................................................................................................................284
Blank Lines...........................................................................................................................................284
Encodings (PEP 263).............................................................................................................................285
Imports ..........................................................................................................................285
Whitespace in Expressions and Statements..................................................................286
Pet Peeves............................................................................................................................................286
Other Recommendations......................................................................................................................287
Comments......................................................................................................................288
Block Comments...................................................................................................................................289
Inline Comments...................................................................................................................................289
Documentation Strings..................................................................................................289
Version Bookkeeping.....................................................................................................290
Naming Conventions......................................................................................................290
Descriptive: Naming Styles...................................................................................................................290
Prescriptive: Naming Conventions........................................................................................................291
Programming Recommendations..................................................................................294
Copyright .......................................................................................................................297
? ? PEP 10: Voting Guidelines...................................................................................299
Abstract.........................................................................................................................299
Rationale........................................................................................................................299
Voting Scores.................................................................................................................299
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
xiv
Copyright .......................................................................................................................300
? ? PEP 20: The Zen of Python..................................................................................301
Abstract.........................................................................................................................301
The Zen of Python..........................................................................................................301
Easter Egg......................................................................................................................301
Copyright .......................................................................................................................302
? ? PEP 257: Docstring Conventions.........................................................................303
Abstract.........................................................................................................................303
Rationale........................................................................................................................303
Specification..................................................................................................................303
What is a Docstring?.............................................................................................................................303
One-Line Docstrings.............................................................................................................................304
Multi-Line Docstrings...........................................................................................................................305
Handling Docstring Indentation............................................................................................................306
Copyright .......................................................................................................................307
Acknowledgments.........................................................................................................307
? ? PEP 387: Backwards Compatibility Policy..........................................................309
Abstract.........................................................................................................................309
Rationale........................................................................................................................309
Backwards Compatibility Rules.....................................................................................309
Making Incompatible Changes ......................................................................................310
Copyright .......................................................................................................................311
? ? PEP 3000: Python 3000 ......................................................................................313
Abstract.........................................................................................................................313
Naming..........................................................................................................................313
PEP Numbering..............................................................................................................313
Download from PookeBook <http://www.pookebook.com>
? CONTENTS
xv
Timeline.........................................................................................................................313
Compatibility and Transition..........................................................................................314
Implementation Language.............................................................................................315
Meta-Contributions........................................................................................................315
Copyright .......................................................................................................................315
? ? PEP 3003: Python Language Moratorium...........................................................317
Abstract.........................................................................................................................317
Rationale........................................................................................................................317
Details............................................................................................................................318
Cannot Change.....................................................................................................................................318
Case-by-Case Exemptions....................................................................................................................318
Allowed to Change................................................................................................................................318
Retroactive.....................................................................................................................319
Extensions .....................................................................................................................319
Copyright .......................................................................................................................319
? ? Index...................................................................................................................321

标签: python python Pro py

实例下载地址

Pro Python

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警