zoukankan      html  css  js  c++  java
  • PEP8编码规范

    1.代码布局设计

    1.1 缩进 -4个空格进行缩进

    1.2 tab键-在python2中tab和空格是混用的,但是在python中基本上使用tab(pycharm开发工具会自动对代码缩进)

    1.3 最大行长度-行的最大长度是79个字符

    1.4 反斜杠-反斜杠在有的时候是适用的,例如在参数很长,但是不能隐式的使用多行的时候,如下反斜杠的使用:

    with open('/path/to/some/file/you/want/to/read') as file_1, 
         open('/path/to/some/file/being/written', 'w') as file_2:
        file_2.write(file_1.read())
    

    1.5 空行 -Top level函数和类的定义的时候,空两行。类中方法的定义空一行。

    1.6 源文件编码 -

    在源文件中一直使用utf-8编码,在python2中使用ascll编码

    文件,在python2中使用ascll编码,在python3中使用utf-8编码

    1.7 导入 - 如 import os,import time 经理单行导入

    2.避免使用空格的情况

    2.1 - 在小括号,中括号,大括号中避免使用空格

    Yes: spam(ham[1], {eggs: 2})
    
    No:  spam( ham[ 1 ], { eggs: 2 } )
    

    2.2 -在逗号,分好,冒号之前不需要空格

    Yes: if x == 4: print x, y; x, y = y, x
    
    No:  if x == 4 : print x , y ; x , y = y , x
    

    2.3 -在切片的时候,避免使用空格,在扩展的切片中,必须使用相同的空格个数

    3.注释

    3.1 块注释

    在一段代码前增加注释,在#后添加一个空格,段落之间只有一个#作为行间隔

    # Description : Module config.
    
    #
    
    # Input : None
    
    #
    
    # Output : None
    

    3.2 行注释

    在使用行注释的时候,在代码句子结束之后至少两个空格,然后用#开头后跟一个空格

    x = x + 1                 # Increment x
    
    But sometimes, this is useful:
    
    x = x + 1                 # Compensate for border
    

    3.3 文档注释

    在所有的公共模块,函数,类,方法中加入文档注释,这些注释写在def之后。

    在进行多行注释的时候,注意“”“结束的时候,必须独占一行,如下:

    """Return a foobang
    
     
    
    Optional plotz says to frobnicate the bizbaz first.
    
    """
    

    4. 类名称

    类名称主要遵循为CapWords约定,表示为首字母大写

    5.1 异常名称

    异常归于类,从而也要遵循类名的规范,主要是在后缀上必须添加“Error“

    5.2 全局变量名

    全局变量只在模块类有效,和function命名相同

    5.3 方法名称

    方法名称全部为小写,下划线是可选的(在增加可读性的基础上使用)

    5.4 方法变量

    类的方法第一个参数总是self

    类方法的静态变量总是为crs

    如果一个方法的参数和保留字相冲突,那么在后面添加下划线进行区分

      

      

      

      

  • 相关阅读:
    Neko's loop HDU-6444(网络赛1007)
    Parameters
    SETLOCAL
    RD / RMDIR Command
    devenv 命令用法
    Cannot determine the location of the VS Common Tools folder.
    'DEVENV' is not recognized as an internal or external command,
    How to change Visual Studio default environment setting
    error signing assembly unknown error
    What is the Xcopy Command?:
  • 原文地址:https://www.cnblogs.com/shaojiafeng/p/8532744.html
Copyright © 2011-2022 走看看