PEP8编码规范

MicroPython相关代码、库、工具
回复
头像
shaoziyang
帖子: 742
注册时间: 2019年 10月 21日 13:48

PEP8编码规范

#1

帖子 shaoziyang » 2019年 11月 5日 10:04

原帖作者microCPyE

This document gives coding conventions for the Python code comprising the standard library in the main Python distribution. Please see the companion informational PEP describing style guidelines for the C code in the C implementation of Python [1].
本文档列出的编码约定,是基于主Python发行版标准库的Python代码的。Python的C语言实现的C代码风格指南,请参阅PEP 7[1]

This document was adapted from Guido's original Python Style Guide essay [2], with some additions from Barry's style guide [3]. Where there's conflict, Guido's style rules for the purposes of this PEP. This PEP may still be incomplete (in fact, it may never be finished <wink>;).
这篇文档改编自Guido(译者注:Python之父,Guido van Rossum)最初的《Python Style Guide》[2]一文,《Barry's style guide》[3]亦有贡献。对(2文)有冲突的地方,Guido的风格指南更符合这份PEP思想。当然,这份PEP仍不完善(实际上,它可能永远也无法完善<wink>)

A Foolish Consistency is the Hobgoblin of Little Minds 愚蠢的一致性就像没脑子的妖怪
One of Guido's key insights is that code is read much more often than it is written. The guidelines provided here are intended to improve the readability of code and make it consistent across the wide spectrum of Python code. As PEP 20 says, "Readability counts".

Guido的关键见解之一是阅读代码比怎么写代码更重要。这份指南意图提高代码的可阅读性,使整个 Python 代码的代码保持一致。正如 PEP 20 所表述的,“可读性很重要”。(译者注:可以点击这里查看由lanphaday翻译的“The Zen of Python”。)

A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.
这是一份关于一致性的风格指南。这份风格指南的风格一致性是非常重要的。更重要的是项目的风格一致性。在一个模块或函数的风格一致性是最重要的。

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!
但最重要的是:知道什么时候是不一致的————有时风格指南并不适用。有疑问时,请使用您的最佳判断。看看其他的例子,并决定什么看上去最美观。不要犹豫,提示提问!

Two good reasons to break a particular rule:
两个很好的理由来打破特定规则:
  • When applying the rule would make the code less readable, even for someone who is used to reading code that follows the rules.
    应用了这个规则后,代码的可读性却降低了。甚至有人依据以下的规则来阅读代码。
  • To be consistent with surrounding code that also breaks it (maybe for historic reasons) -- although this is also an opportunity to clean up someone else's mess (in true XP style).
    欲与周围的代码的代码保持一致,也会打破这个规则(也许是历史的原因)————虽然这也是一个机会来收拾别人的烂摊子(真正的XP风格)。(译者注:XP,Xtreme Programming,极限编程)


===========================================
相关链接:
Python PEP8编码规范在线检查器 PEP8官方网站: 中文翻译:

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章