Python Coding Style - Python 编码风格

Python Coding Style - Python 编码风格

More Control Flow Tools
https://docs.python.org/3.7/tutorial/controlflow.html

其他流程控制工具
https://docs.python.org/zh-cn/3.7/tutorial/controlflow.html

intermezzo [ˌɪntəˈmetsəʊ]:n. 间奏曲,插曲,幕间剧,芭蕾舞表演
concise [kənˈsaɪs]:adj. 简明的,简洁的
tremendously [trəˈmendəsli]:adv. 非常地,可怕地,惊人地
emerge [ɪˈmɜːdʒ]:vi. 浮现,摆脱,暴露
adhere [ədˈhɪə(r)]:vi. 坚持,依附,粘着,追随 vt. 使粘附
eye-pleasing:养眼的

Now that you are about to write longer, more complex pieces of Python, it is a good time to talk about coding style. Most languages can be written (or more concise, formatted) in different styles; some are more readable than others. Making it easy for others to read your code is always a good idea, and adopting a nice coding style helps tremendously for that.
现在你将要写更长,更复杂的 Python 代码,是时候讨论一下代码风格了。大多数语言都能以不同的风格被编写 (或更准确地说,被格式化);有些比其他的更具有可读性。能让其他人轻松阅读你的代码总是一个好主意,采用一种好的编码风格对此有很大帮助。

For Python, PEP 8 has emerged as the style guide that most projects adhere to; it promotes a very readable and eye-pleasing coding style. Every Python developer should read it at some point; here are the most important points extracted for you:
对于 Python,PEP 8 已经成为大多数项目所遵循的风格指南;它促进了一种非常易读且令人赏心悦目的编码风格。每个 Python 开发人员都应该在某个时候阅读它;以下是为你提取的最重要的几个要点:

  • Use 4-space indentation, and no tabs. (使用4个空格缩进,不要使用制表符。)

4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.
4 个空格是一个在小缩进 (允许更大的嵌套深度) 和大缩进 (更容易阅读) 的一种很好的折中方案。制表符会引入混乱,最好不要使用它。

compromise [ˈkɒmprəmaɪz]:n. 妥协,和解,妥协 (或折中) 方案,达成妥协 v. 妥协,折中,违背 (原则),达不到 (标准),(因行为不当) 使陷入危险,名誉受损
indentation [ˌɪndenˈteɪʃn]:n. 压痕,刻痕,凹陷,缩排,呈锯齿状
  • Wrap lines so that they don’t exceed 79 characters. (换行,使一行不超过 79 个字符。)

  • This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.
    这有助于使用小型显示器的用户,并且可以在较大的显示器上并排放置多个代码文件。

  • Use blank lines to separate functions and classes, and larger blocks of code inside functions.
    使用空行分隔函数和类,以及函数内的较大的代码块。

  • When possible, put comments on a line of their own.
    如果可能,把注释放到单独的一行。

  • Use docstrings.
    使用文档字符串。

  • Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).
    在运算符前后和逗号后使用空格,但不能直接在括号内使用:a = f(1, 2) + g(3, 4)

  • Name your classes and functions consistently; the convention is to use UpperCamelCase for classes and lowercase_with_underscores for functions and methods. Always use self as the name for the first method argument (see A First Look at Classes for more on classes and methods).
    以一致的规则为你的类和函数命名;按照惯例应使用 UpperCamelCase 来命名类,而以 lowercase_with_underscores 来命名函数和方法。始终应使用 self 来命名第一个方法参数 (有关类和方法的更多信息请参阅初探类)。

  • Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case.
    如果你的代码旨在用于国际环境,请不要使用花哨的编码。Python 默认的 UTF-8 或者纯 ASCII 在任何情况下都能有最好的表现。

  • Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.
    同样,哪怕只有很小的可能,遇到说不同语言的人阅读或维护代码,也不要在标识符中使用非 ASCII 字符。

bracket [ˈbrækɪt]:n. 支架,墙上凸出的托架,括号,等级,范围 vt. 括在一起,把...归入同一类,排除
construct [kənˈstrʌkt]:v. 建造,修建,组成,创建,编制,绘制,形成 (观念,理论) n. 构想,概念,编造,杜撰,(短语的) 结构成分,结构体,建造物,构筑物
fancy ['fænsi]:v. 想象,竟然,认为,想要 n. 爱好,想象的事物,想象,想要 adj. 异常复杂的,太花哨的,精致的,有精美装饰的

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值