Effective Python: 59 Specific Ways to Write Better Python
Tags
Summary
Editor's review
Who it's for
A good fit for
- 已掌握Python基础语法、能独立写脚本,想进一步提升代码质量的开发者
- 从其他语言转来、想弄清Python惯用法与语言机制的工程师
- 准备技术面试、需要系统梳理Python进阶知识点的求职者
- 带团队的技术负责人,可用作代码评审与内部培训的参考条款
Not a good fit for
- 完全没有编程经验的零基础读者,书中不讲解变量、循环等入门知识
- 只想要一份快速上手的Python教程或速查手册的人
- 需要覆盖Python 3.10以上新特性(如match、类型注解新语法)的读者
Reading tips
- 不必从头读到尾,先按兴趣跳读条款,遇到与项目相关的主题再精读并跑通示例代码
- 读每条时先自己写一版实现,再对照书中的反例,体会差异出在哪里
- 配合官方文档阅读第3、4章,把描述符、元类、__getattr__的机制真正弄懂
- 注意书中兼顾Python 2.x的写法,练习时改用当前Python 3,并留意与第二版的差异
About the publisher
In addition to science and technology and professional publishing, the books in our catalog from China Machine Press also reflect a sustained focus on psychology, computing, management, finance and economics, and social sciences. In psychology, the catalog includes works by C. G. Jung and introductions to analytical psychology, as well as applied topics such as stress and health, and addiction and healing. In computing, it features Structure and Interpretation of Computer Programs (JavaScript Edition). Management and finance titles include Administrative Behavior (4th Edition), The Price of Inequality (Collector's Edition), and This Time Is Different: Eight Centuries of Financial Folly (Commemorative Edition). In the philosophy and history of science, there is The Worldview: The Third Edition. Relevant authors include C. G. Jung, William McGuire, Sonu Shamdasani, and Gabor Maté. Public information is limited; the above summarizes the publisher's profile based solely on the books in our catalog.
Contents
- Praise for the Book
- Translator's Preface
- Foreword
- Acknowledgments
- Chapter 1: Think in Pythonic Ways
- Item 1: Confirm the Python Version You Are Using
- Item 2: Follow the PEP 8 Style Guide
- Item 3: Know the Differences Between bytes, str and unicode
- Item 4: Use Helper Functions Instead of Complex Expressions
- Item 5: Know How to Slice Sequences
- Item 6: Avoid Specifying start, end and stride Together in a Single Slice
- Item 7: Use List Comprehensions Instead of map and filter
- Item 8: Avoid List Comprehensions with More Than Two Expressions
- Item 9: Use Generator Expressions to Rewrite List Comprehensions on Large Data
- Item 10: Prefer enumerate to range
- Item 11: Use zip to Iterate Two Iterators at Once
- Item 12: Avoid else Blocks After for and while Loops
- Item 13: Use Each Block in try/except/else/finally Wisely
- Chapter 2: Functions
- Item 14: Prefer Exceptions to Returning None for Special Cases
- Item 15: Know How to Use Variables from the Enclosing Scope in Closures
- Item 16: Consider Generators Instead of Functions That Return Lists Directly
- Item 17: Be Careful When Iterating Over Arguments
- Item 18: Use Variable Positional Arguments to Reduce Visual Noise
- Item 19: Use Keyword Arguments to Express Optional Behavior
- Item 20: Use None and Docstrings to Describe Parameters with Dynamic Default Values
- Item 21: Use Keyword-Only Arguments to Ensure Clarity
- Chapter 3: Classes and Inheritance
- Item 22: Prefer Helper Classes to Dictionaries and Tuples for Maintaining Program State
- Item 23: Simple Interfaces Should Accept Functions, Not Instances of Classes
- Item 24: Use @classmethod Polymorphism to Construct Objects Generically
- Item 25: Initialize Parent Classes with super
- Item 26: Use Multiple Inheritance Only for Mix-in Utility Classes
- Item 27: Prefer Public Attributes to Private Attributes
- Item 28: Inherit from collections.abc to Implement Custom Container Types
- Chapter 4: Metaclasses and Attributes
- Item 29: Use Plain Attributes Instead of Get and Set Methods
- Item 30: Consider @property Instead of Refactoring Attributes
- Item 31: Use Descriptors to Rewrite Reusable @property Methods
- Item 32: Use __getattr__, __getattribute__ and __setattr__ to Implement Attributes Generated on Demand
FAQ
这本书和《Effective Python》第二版有什么区别?
零基础可以直接读这本书吗?
书中的代码在现在的Python版本上还能运行吗?
59条建议需要全部背下来吗?
Comments
Find this book
This site is an index: you start “Search all sources” in your browser, and the entries it lists all point to third-party sites; please judge their availability yourself.