编码
介绍
在无特殊情况下,文件一律使用UTF-8编码,文件头加入#-*-coding:utf-8-*-标识
代码示例
命名
变量命名
介绍
 在Python编程中对变量进行命名时,单个单词全部小写,多个单词时使用蛇形命名法
蛇形命名法
单词全部小写,每个单词使用下划线进行连接
1 2
   | guest_username = "user1"		 guest_password = "123456"		
   | 
 
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13
   |  username = "admin" password = "123456" guest_username = "user1"		 guest_password = "123456"		 global_username = "admin"		
 
 
  a = "admin" b = "123456" guestusername = "user1" guestpassword = "123456"
 
  | 
 
常量命名
介绍
在Python编程中对常量(配置文件变量、不修改的变量)进行命名时,单词全部字母大写
代码示例
单词全部大写,每个单词使用下划线进行连接
1 2 3 4
   | PORT = 80 ADDRESS = 127.0.0.1 USER_FILE = "/xx.conf" GLODAL_CONF_FILE = "/xx.conf"	
   | 
 
函数命名
介绍
在Python编程中对函数进行命名时,与变量命名一样,使用蛇形命名法
代码示例
单词全部小写,每个单词使用下划线进行连接
1 2 3 4 5 6 7 8 9 10
   | def hello():     pass
  def write_log():     pass
 
 
  def _private_func():     pass
   | 
 
类命名
介绍
在Python编程中对类进行命名时,使用大驼峰命名法
大驼峰命名法
每个单词以大写字母开头
1 2 3 4 5 6 7 8 9 10 11
   |  class Student(object):     pass
  class WriteFile(object):     pass
 
 
  class _Private_cls(object):     pass
 
  | 
 
文件命名
介绍
对Python文件命名时应该是描述性的,尽量避免缩进,不要通过删除单词中的字母来进行缩写,多单词时使用下划线分隔
命名示例
1 2 3 4 5 6 7 8 9
   | # 规范命名 request_url.py get_response.py download.py
  # 不规范命名 a.py get-resp.py DOWNLOAD.py
   | 
 
缩进
代码缩进
介绍
使用4个空格来缩进代码,不提倡使用Tab键进行缩进,也不要Tab与空格混用
代码示例
在不同编辑器中对Tab键的解释是不同的,有时候一个Tab等于4个或8个空格,如果Tab键与空格混用在不同编辑器中则可能出现代码不会对齐,导致代码运行错误
1 2 3 4 5 6 7 8 9
   |  for i in range(10):     print(i)     break
 
  for i in range(10): 	print(i)	     break		
 
  | 
 
序列缩进
介绍
当]、)、}和末位元素在不同一行时推荐使用序列元素尾部逗号
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   |  users = ['jack','tom','jerry'] users = [     'jack',     'tom',     'jerry',	 ]
 
  users = [     'jack',     'tom',     'jerry'		 ]
 
  | 
 
注释
文档注释
介绍
文档注释一般出现在模块、函数、类的头部,文档注释以"""开头结尾。出现多行注释时首行不换行,末行必换行。文档注释不是越长越好,能一两句话写清楚最好
模块注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   |  """文档注释演示
  阿巴阿巴阿巴阿巴阿巴阿巴 阿巴阿巴阿巴阿巴阿巴阿巴
  示例: 	演示使用的运行环境为Windows10 & Python3.7 	$ python file.py -H 127.0.0.1 -P 8080 		-H 指定需要扫描的主机IP 		-P 指定要扫描的端口 	 """ import xxx
  代码块
 
  | 
 
函数注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | def Student():     """对学生姓名的排序处理,以列表的方式返回结果"""     pass
 
  def Student(name, age):     """对学生信息的入库,返回执行状态          参数     --------     name: str     	学生姓名     age: int     	学生年龄     	 	返回值     --------     Bool     	返回True为执行成功,返回False为执行失败          """     pass
   | 
 
类注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | class School(object):     """类的文档注释演示
      阿巴阿巴阿巴阿巴阿巴阿巴     阿巴阿巴阿巴阿巴阿巴阿巴
      公共属性:         student_name: 学生姓名         student_age: 学生年龄     """
 
      def __init__(self, student_name, student_age):         """初始化School类"""         self.student_name = student_name         self.student_age = student_age
 
      def Student(self):         """将学生信息入库"""         pass
 
   | 
 
块注释
介绍
最需要写注释的时代码中那些技巧性的部分。对于复杂操作应当在其开始前协商若干行注释
代码示例
1 2 3 4 5 6 7 8 9
   | 
 
 
  if flag == "Success": 	for user in users: 		假装在排序 	假装在去重处理 	假装在写入数据库
 
  | 
 
行注释
介绍
对于不是一目了然的代码,在其行尾添加上注释。为提高可读性,注释应该至少离开代码两个空格。
代码示例
醒目注释
介绍
在代码的关键部分或较为复杂的地方写上醒目的注释,突出重要性
代码示例
空行
介绍
顶级定义(函数或类)之间空两行,方法定义(类成员函数)之间空一行
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | class Student(object):
      def __init__(self):         pass
      def run(self):         pass
 
  class Teacher(object):
      def __init__(self):         pass
      def run(self):         pass
 
  def main():     pass
 
   | 
 
空格
介绍
- 在二元运算符两边各空一格 
[=,-,+=,==,>,in,is not, and] 
- 函数的参数列表中,
,之后空一格,默认值等号两边要空格 
- 括号内不要有空格,不要在逗号、分号、冒号前面加空格
 
- 参数列表、索引、切片的左括号前不加空格
 
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   |  a = 1 + 1 * 1 / 1 a = [1, 2] a = {'a': 2} a['key'] = b['xx']
  def test(name, age=10, color):     pass
 
  a = 1+1*1/1 a = [ 1,2 ] a = { 'a':2 } a ['key'] = b ['xx']
  def test(name,age = 10,color):     pass
 
  | 
 
行长
介绍
每行不超过80个字符,除导入长的模块语句、注释里的URL。如果字符串过长可使用小括号连接起来
代码示例
1 2 3 4 5 6 7
   |  if (width == 0 and height == 0 and     color == 'red' and emphasis == 'strong'):     
 
  if width == 0 and height == 0 and color == 'red' and emphasis == 'strong':
 
  | 
 
1 2 3 4 5 6 7 8
   |  txt = ("xxxxxxxxxxxxxxxxxxxx"        "xxxxxxxxxxxxxxxxxxxx"        "xxxxxxxxxxxxxxxxxxxx")          
  txt = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 
  | 
 
字符串
介绍
- 参数都是字符串时可以使用
%操作符或格式化方法格式化字符串,具体还是根据现实情况来判断使用+还是%操作符或格式化方法 
- 在循环中需要对字符串进行叠加时可以将字符串写入到列表,在循环结束后使用
join进行拼接,情况不合适时在考虑使用+=拼接 
- 在同一文件中时要保持字符串引号的一致性,使用单引号或双引号之一用于引用字符串,并在同一文件中沿用,在字符串中就可以使用另外一种引号
 
代码示例
1 2 3 4 5 6 7 8 9 10 11
   |  txt = a + b txt = '%s,%s' % (a,b) txt = '{},{}'.format(a,b) txt = 'name: %s;age: %d' % (a,b) txt = 'name: {};age: {}'.format(a,b)
 
  txt = '%s%s' % (a,b)		 txt = '{}{}'.format(a,b)	 txt = 'name: ' + a + 'age: ' + b
 
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   |  txt_list = [] data = {'a':'Hello','b':'World'} for t in data.values():     txt_list.append(t) txt = ''.join(txt_list) print(txt)
 
 
  txt = '' data = {'a':'Hello','b':'World'} for t in data.values():     txt += t print(txt)
 
  | 
 
1 2 3 4 5 6 7 8 9
   |  a = 'HelloWorld' b = "I'm iron man" c = '"Good!" thought a happy Python reviewer.'
 
  a = 'HelloWorld' b = "Jack" c = 'Jerry'
 
  | 
 
类
介绍
如果一个类不继承自其他类,就继承object类,嵌套类也一样
代码示例
1 2 3 4 5 6 7
   |  class Student(object):     pass
 
  class Student:     pass
 
  | 
 
TODO注释
介绍
TODO主要是为了说明待做任务,任务标明任务人、联系方式、任务时间等信息
代码示例
TODO注释应当在所有开头处包含TOPO字符串,后面使用()括起你的名字、email或其他标识符,然后是一个可选的:,后面加上注释,解释要做什么
1 2 3 4 5 6 7
   | def get_response():          pass
  def get_userinfo():          pass
   | 
 
导入格式
介绍
- 导入语句放在文件头部,模块说明之后,全局变量之前
 
- 导入顺序按照最通用到最不通用的顺序分组(标准库导入、第三方库导入、应用程序指定导入),每组之间用一个空行分隔
 
- 每个导入应当独占一行
 
代码示例
import应当分行书写,每个导入应独占一行
1 2 3 4 5 6 7
   |  import os import sys from subprocess import Popen, PIPE
 
  import os,sys
 
  | 
 
Main
介绍
- 即使是一个被打算作为脚本的文件,也应该时可以导入的
 
- 并且简单的导入不应该导致这个脚本的主功能被执行,主功能应该放在
main()函数中 
- 代码在执行主程序前总是检查模块名,当模块被导入时就不会被执行
 
代码示例
__namne__是当前模块名,当模块被直接运行时模块名为__main__,将相关的调用或执行放在if __name__ == "__main__"当中,当该模块被导入时就不会被执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   |  def add():     pass
  def main():	     pass
 
  if __name__ == '__main__':     main()   
 
  def add():     pass
  主功能xxxx
 
  | 
 
参考地址: 
https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/#
https://www.runoob.com/w3cnote/google-python-styleguide.html
guide.html