Python good book recommends "Python code neat way" -- writing elegant code

ztenv 2020-11-13 12:59:49
python good book recommends python


Python It's one of the most popular languages today . Relatively new fields like data science 、 Artificial intelligence 、 Robots and data analysis , And traditional majors like Web Development and scientific research , All embracing Python. as time goes on ,Python It may develop into a basic discipline , therefore , Learn from good examples Python It is a necessary skill for survival and development in some fields .

For using Python For programmers who write code in such a dynamic language , It's becoming more and more important to ensure high quality and error free code . As a Python Developer , You want to make sure that the software you're building will satisfy users , It doesn't go over budget or can't be released .

at present Python What's missing in is code consistency 、 Patterns and developers are good at Python Code understanding . For each Python The programmer , good Python Code has different meanings . The reason for this may be Python Used in so many fields , It's so hard for developers to agree on a particular pattern .

Python It's a simple language , But it's hard to write good code , Because it can teach us to write better Python Code resources are rare . If you want to skillfully write neat Python Code , And can successfully apply these principles to your own Python In the project , I recommend you to read 《Python Clean code : Write elegant code 》 A Book .

 Insert picture description here

 Insert picture description here
For details, see

The main purpose of this book is for different levels Python Developers provide skills , So that they can write better Python Software and programs . No matter what field you use Python, This book can provide you with a variety of techniques . This book covers all levels from basic to advanced Python knowledge , And shows you how to make the code more consistent with Python Style .

please remember , Writing software is not just a science , And it's an art , This book will teach you how to be a better Python The programmer .

Let's watch it first

Content abstract

Explore the use of Python The right way to write code . This book provides error free and robust Python The skills and techniques needed for the project .

To teach you how to write better code , This book first introduces the importance of understanding code formatting and code annotation , And using built-in data structures and Python Dictionaries improve maintainability , Use modules and metaclasses to organize code effectively ;

And then I'll go into it Python The new features of language , And teach readers how to use them effectively ; Next , Some key concepts will be introduced in depth , Such as asynchronous programming 、Python data type 、 Type prompt and path handling, etc , And talk about debugging 、 Unit testing and integration testing skills , To ensure that the code can be put into production ;

Finally, in the appendix, we introduce some excellent examples that can help speed up the development and improve the quality of the code Python Tools .

After reading this book , You will skillfully write neat Python Code , And can successfully apply these principles to your own Python In the project .

By reading this book , You will learn the following

(1) How to write neat Python Code .

(2)Python Data structure and characteristics of .

(3)Python The function in 、 Classes and modules ( Modules are not mentioned or simply mentioned in many books , This book has a more detailed explanation ).

(4) Decorator 、 generator 、 The role and usage scenarios of iterator and context manager .

(5)Python 3.x Some of the new features in , Such as async And the process 、 Type tagging, etc .

(6) Some tools for debugging and unit testing .

Author's brief introduction

Sunil Kapil In the past 10 I have been engaged in software development during the year , use Python And several other languages to write code , Mainly involves Web And software development for mobile services . He develops 、 Deployed and maintained projects loved and used by millions of users , These projects are done in collaboration with teams from different professional environments , Involving world famous software companies . He's also a passionate advocate of open source , And continue to contribute Zulip Chat and Black Projects such as . He also works with nonprofits , And contribute to their software projects as volunteers .

Translator introduction

Lian Shaohua , He worked in ZTE successively 、 The shenzhen stock exchange 、 Jinzheng shares and other well-known companies and institutions , Keen on software business , The technology stack is extensive , involve C++、C#、Java、Python、Golang etc. , Deep understanding and practice of architecture design and underlying technology , It has been submitted to some open source libraries abroad bug And contributed code . stay CSDN The forum served as 5 Years of C++ The moderator of the small edition and C/C++ The moderator of the big edition . Translation have 《C++ Clean code 》, Now we are committed to the design and development of big data platform .


● The first 1 Chapter About Python Thinking 1

1.1 To write Python Code 1

1.1.1 name 2

1.1.2 Expressions and statements in code 5

1.1.3 hug Python How to write code 8

1.2 Use the document string 14

1.2.1 Module level document string 17

1.2.2 Make the class document string descriptive 17

1.2.3 Function document string 18

1.2.4 Some useful document string tools 19

1.3 To write Python Control structure of 20

1.3.1 Use list derivation 20

1.3.2 Don't use complex list derivation 21

1.3.3 You should use lambda Do you 23

1.3.4 When to use generators and when to use list derivation 23

1.3.5 Why not use... In a loop else24

1.3.6 Why? range Function in Python 3 Better 27

1.4 Trigger exception 28

1.4.1 Habits lead to anomalies 28

1.4.2 Use finally To handle exceptions 30

1.4.3 Create your own exception class 31

1.4.4 Only handle specific exceptions 32

1.4.5 Be careful of third party anomalies 34

1.4.6try Minimum code blocks 35

1.5 Summary 36

● The first 2 Chapter data structure 38

2.1 Common data structure 38

2.1.1 Use set 38

2.1.2 Use when returning and accessing data namedtuple40

2.1.3 understand str、Unicode and byte43

2.1.4 Use lists with caution , Give priority to generators 44

2.1.5 Use zip Process list 47

2.1.6 Use Python Built in functions for 48

2.2 Using dictionaries 50

2.2.1 When to use dictionaries and when to use other data structures 51


2.2.3 Orderly dictionary 、 Default dictionary 、 General dictionary 54

2.2.4 Using dictionary switch sentence 55

2.2.5 How to combine two dictionaries 56

2.2.6 Print dictionaries gracefully 57

2.3 Summary 58

● The first 3 Chapter Write better functions and classes 59

3.1 function 59

3.1.1 Write small functions 60

3.1.2 Return to generator 61

3.1.3 Throw an exception instead of returning None63

3.1.4 Use default parameters and keyword parameters 64

3.1.5 Don't explicitly return to None66

3.1.6 Be careful when writing functions 68

3.1.7 Use alone lambda expression 70

3.2 class 72

3.2.1 Class size 72

3.2.2 Class structure 73

3.2.3 Use... Correctly @property75

3.2.4 When to use static methods 77

3.2.5 Inherited abstract class 79

3.2.6 Use @classmethod To access the state of a class 80

3.2.7 Use public properties instead of private properties 81

3.3 Summary 83

● The first 4 Chapter Use modules and metaclasses 84

4.1 Modules and metaclasses 84

4.2 How to use modules to organize code 86

4.3 Use __init__ file 88

4.4 Import functions and classes from modules in the right way 90

4.5 When to use metaclasses 92

4.6 Use __new__ Method validation subclass 93

4.7__slots__ Use of 95

4.8 Use metaclasses to change the behavior of a class 98

4.9Python The descriptor 100

4.10 Summary 102

● The first 5 Chapter Decorators and context managers 104

5.1 Decorator 105

5.1.1 Ornament and its function 105

5.1.2 Understand decorators 106

5.1.3 Use decorators to change behavior 108

5.1.4 Using multiple decorators at the same time 110

5.1.5 Using decorators with parameters 111

5.1.6 Consider using the decorator Library 112

5.1.7 Class decorators for maintaining state and validation parameters 114

5.2 Context manager 117

5.2.1 Context manager and its purpose 117

5.2.2 Understand the context manager 119

5.2.3 Use contextlib Create context manager 120

5.2.4 An example of a context manager 121

5.3 Summary 124

● The first 6 Chapter Generators and iterators 125

6.1 Using generators and iterators 125

6.1.1 Understanding iterators 125

6.1.2 What is a generator 128

6.1.3 When to use iterators 129

6.1.4 Use itertools130

6.1.5 Why generators are so useful 132

6.1.6 List derivation and iterators 133

6.2 Use yield keyword 133

6.2.1yield from135

6.2.2yield Faster than data structures 135

6.3 Summary 136

● The first 7 Chapter Use Python New features 137

7.1 Asynchronous programming 137

7.1.1Python Medium async138

7.1.2asyncio How it works 141

7.1.3 Asynchronous generator 151

7.2 type annotation 159

7.2.1Python The type of 160

7.2.2typing modular 160

7.2.3 Does type checking affect performance 163

7.2.4 How type annotations help write better code 163

7.2.5typing The trap of 163

7.3super() Method 164

7.4 Type tips 164

7.5 Use pathlib Processing path 164

7.6print() Now it's a function 165


7.8 Key parameters 166

7.9 Keep the dictionary data in order 166

7.10 Iterative unpacking 166

7.11 Summary 167

● The first 8 Chapter Commissioning and testing Python Code 168

8.1 debugging 168

8.1.1 Debugging tools 169


8.1.3 Use... In product code logging Module replacement print172

8.1.4 Use metrics Library to analyze performance bottlenecks 177

8.1.5IPython What's the help 178

8.2 test 179

8.2.1 Testing is very important 179

8.2.2Pytest and UnitTest180

8.2.3 Property testing 184

8.2.4 Generate test reports 184

8.2.5 Automated unit testing 185

8.2.6 Prepare the code for production 186

8.2.7 stay Python Execution unit and integration test in 186

8.3 Summary 189

appendix Some great Python Tools 190


  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database