The idioms of a programming language are defined by its users. Over the years, the Python community has come to use the adjective Pythonic to describe code that follows a particular style. The Pythonic style isn’t regimented or enforced by the compiler. It has emerged over time through experience using the language and working with others. Python programmers prefer to be explicit, to choose simple over complex, and to maximize readability. (Type import this into your interpreter to read The Zen of Python.)
编程语言的习惯用法是由用户定义的。多年来,Python社区已经开始使用形容词Pythonic来描述遵循某种特定风格的代码。Pythonic风格不受编译器的约束或强制,它是通过长期的使用语言和与他人合作的经验中逐渐出现的编程范式。Python程序员更喜欢显示的、简洁而不是复杂的、高可读性的代码风格。(在您的解释器里输入 import this 阅读Python之禅。)
Programmers familiar with other languages may try to write Python as if it’s C++, Java, or whatever they know best. New programmers may still be getting comfortable with the vast range of concepts that can be expressed in Python. It’s important for you to know the best—the Pythonic—way to do the most common things in Python. These patterns will affect every program you write.
熟悉其他语言的程序员可能会尝试编写Python,好像它是C++、Java或任何他们最了解的语言。新程序员可能仍然对可以用Python表达大量的概念感到舒服。重要的是,你要知道最好的——在Python中做最常见事情的Pythonic方式。这些模式会影响你编写的每一个程序。
Item 1: Know Which Version of Python You’re Using
Item 2: Follow the PEP 8 Style Guide
Item 3: Know the Differences Between bytes and str
Item 4: Prefer Interpolated F-Strings Over C-style Format Strings and str .format
Item 5: Write Helper Functions Instead of Complex Expressions
Item 6: Prefer Multiple Assignment Unpacking Over Indexing
Item 7: Prefer enumerate Over range
Item 8: Use zip to Process Iterators in Parallel
Item 9: Avoid else Blocks After for and while Loops
Item 10: Prevent Repetition with Assignment Expressions
第1项:了解您正在使用的Python版本
第2项:遵循PEP 8风格指南
第3项:了解bytes和str之间的区别
第4项:使用插值的f字符串,而不是c风格的格式字符串和str.format
第5项:用辅助函数代替复杂表达式
第6项:优先选择多重赋值解包而不是索引
第7项:优先使用enumerate而不是range
第8项:使用zip并行处理迭代器
第9项:避免在for和while循环之后使用else
第10项:使用赋值表达式防止重复
waiting for updating...