摘自google.
https://i.cnblogs.com/PostDone.aspx?postid=9753605&actiontip=%E4%BF%9D%E5%AD%98%E4%BF%AE%E6%94%B9%E6%88%90%E5%8A%9F
1、缩进
Tip
用4个空格来缩进代码
2、代码太长:
如果一个文本字符串在一行放不下, 可以使用圆括号来实现隐式行连接:
x = ('This will build a very long long '
'long long long long long long string')
或者:
print \\
"hello world!"
3、类:
class SampleClass(object):
4、注释:
"""Explicitly inherits from another class already.""" 或者 #Explicitly inherits from another class already.
5、字符串:
通常可以用+,%,join(), format 三个函数进行处理
%s #字符串
%d #数字
Yes: x = a + b
x = '%s, %s!' % (imperative, expletive)
x = '{}, {}!'.format(imperative, expletive)
x = 'name: %s; score: %d' % (name, n)
x = 'name: {}; score: {}'.format(name, n)
Join:
items = ['ni','hao'];
employee_table = ''.join(items);
6、处理文件:
1、with open("hello.txt") as hello_file:
for line in hello_file:
print line
2、import contextlib with contextlib.closing(urllib.urlopen("http://www.python.org/")) as front_page:
for line in front_page:
print line
7、变量等命名规范:
Python之父Guido推荐的规范 Type Public Internal
Modules lower_with_under _lower_with_under
Packages lower_with_under ---------
Classes CapWords _CapWords
Exceptions CapWords --------
Functions lower_with_under() _lower_with_under()
Global/Class Constants CAPS_WITH_UNDER _CAPS_WITH_UNDER
Global/Class Variables lower_with_under _lower_with_under
Instance Variables lower_with_under _lower_with_under (protected) or __lower_with_under (private)
Method Names lower_with_under() _lower_with_under() (protected) or __lower_with_under() (private)
Function/Method Parameters lower_with_under
Local Variables lower_with_under