Python 拾遗 (4)

当你在编写更长更复杂的Python程序是,有一个良好的代码风格会让你事半功倍。大多数的编程语言的格式可以有很多种风格,(c语言中缩进有喜欢打4格的,也有喜欢打3格和2格的)其中的某些风格比另一些可读性更强,就成为大家约定的规则。 让别人能十分容易地看懂你的代码非常重要, 而良好的代码风格则是达成上述目的的必要条件。

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:

1 使用四个空格来缩进,不要用tab

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.

2 如果一行代码超过79个字符,换一行

This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.

3 使用空行把函数和类的定义与其他代码隔开来, 还有函数内部较大的代码块也是如此

4 如果可以,尽量把注释放在一行

5 使用docstrings

6 在操作符两边加空格,在逗号后面加空格,但是左括号右边和右括号左边没有空格

Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).

7 有一种风格来命名类和函数,习惯上我们用CamelCase风格来命名类,命名函数和方法通常由下划线分隔的小写单词命名。方法的第一个参数写self。

(see A First Look at Classes for more on classes and methods).

8 不要使用奇奇怪怪的encoding格式,请使用ASCII编码,什么情况都能用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值