Language Rules

pylint

A tool for finding bugs and style problems

1
2
3
4
5
6
7
#生成默认配置文件
pylint --persistent=n --generate-rcfile > pylint.conf
#Check单个文件
pylint --rcfile=pylint.conf manage.py
#Check整个工程
#在工程根目录下添加init.py文件,可以对整个工程进行pylint。
pylint project

yield

1
2
3
4
5
6
# a generator that yields items instead of returning a list
def firstn(n):
num = 0
while num < n:
yield num
num += 1

lambda

Okay for one-liners. 不用为简单函数取名字。

They are often used to define callbacks or operators for higher-order functions like map() and filter().

For example

1
2
3
4
items = [1, 2, 3, 4, 5]
squared = []
for i in items:
squared.append(i**2)

With map and lambda

1
2
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))

filter and lambda

1
2
3
number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)

太复杂,还是loop好了

Style Rules

程序的main文件应该以 #!/usr/bin/python2或者 #!/usr/bin/python3开始.

在导入模块时, 将会被忽略. 因此只有被直接执行的文件中才有必要加入#!.

‘ ‘ 和 ‘=’ 要注意

缩进

Naming

NAMING