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

    pep8 编码规范

    Python Enhancement Proposals :python改进方案

    https://www.python.org/dev/peps/

     

    1. 每级缩进用4个空格。

    括号中使用垂直隐式缩进或使用悬挂缩进。后者应该注意第一行要没有参数,后续行要有缩进。

    #1. 对准左括号
    foo = long_function_name(var_one, var_two,
                             var_three, var_four)
    
    
    #2. 不对准左括号,但加多一层缩进,以和后面内容区别。
    def long_function_name(
            var_one, var_two, var_three,
            var_four):
        print(var_one)
    
    
    #3. 悬挂缩进必须加多一层缩进.
    foo = long_function_name(
        var_one, var_two,
        var_three, var_four)

    2.if语句跨行时,两个字符关键字(比如if)加上一个空格,再加上左括号构成了很好的缩进。

    后续行暂时没有规定,至少有如下三种格式,建议使用第3种。

    # 没有额外缩进,不是很好看,个人不推荐.
    if (this_is_one_thing and
        that_is_another_thing):
        do_something()
    
    # 添加注释
    if (this_is_one_thing and
        that_is_another_thing):
        # Since both conditions are true, we can frobnicate.
        do_something()
    
    # 额外添加缩进,推荐。
    # Add some extra indentation on the conditional continuation line.
    if (this_is_one_thing
            and that_is_another_thing):
        do_something()

    3.右边括号也可以另起一行。有两种格式,建议第2种。

    # 右括号不回退,个人不推荐
    my_list = [
        1, 2, 3,
        4, 5, 6,
        ]
    result = some_function_that_takes_arguments(
        'a', 'b', 'c',
        'd', 'e', 'f',
        )
    
    # 右括号回退
    my_list = [
        1, 2, 3,
        4, 5, 6,
    ]
    result = some_function_that_takes_arguments(
        'a', 'b', 'c',
        'd', 'e', 'f',
    )

    4. 空格或Tab?

    • 空格是首选的缩进方法。
    • Tab仅仅在已经使用tab缩进的代码中为了保持一致性而使用。
    • Python 3中不允许混合使用Tab和空格缩进。
    • Python 2的包含空格与Tab和空格缩进的应该全部转为空格缩进。

    5. 最大行宽

    • 限制所有行的最大行宽为79字符。
    • 文本长块,比如文档字符串或注释,行长度应限制为72个字符。

    6.空行

    • 两行空行分割顶层函数和类的定义。
    • 类的方法定义用单个空行分割。
    • 额外的空行可以必要的时候用于分割不同的函数组,但是要尽量节约使用。
    • 额外的空行可以必要的时候在函数中用于分割不同的逻辑块,但是要尽量节约使用。

    7.源文件编码

    • 在核心Python发布的代码应该总是使用UTF-8(ASCII在Python 2)。
    • Python 3(默认UTF-8)不应有编码声明。

    8.导入包

    Yes:

    #标准库
    import os  
    import sys
    
    #第三方
    from subprocess import Popen, PIPE
    
    #我自己的
    import video

    括号里边避免空格

    # 括号里边避免空格
    # Yes
    spam(ham[1], {eggs: 2})
    # No
    spam( ham[ 1 ], { eggs: 2 } )



    逗号,冒号,分号之前避免空格

    # 逗号,冒号,分号之前避免空格
    # Yes
    if x == 4: print x, y; x, y = y, x
    # No
    if x == 4 : print x , y ; x , y = y , x


    索引操作中的冒号当作操作符处理前后要有同样的空格(一个空格或者没有空格,个人建议是没有。)

    # Yes
    ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
    ham[lower:upper], ham[lower:upper:], ham[lower::step]
    ham[lower+offset : upper+offset]
    ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
    ham[lower + offset : upper + offset]
    # No
    ham[lower + offset:upper + offset]
    ham[1: 9], ham[1 :9], ham[1:9 :3]
    ham[lower : : upper]
    ham[ : upper]

    函数调用的左括号之前不能有空格

    # Yes
    spam(1)
    dct['key'] = lst[index]
    
    # No
    spam (1)
    dct ['key'] = lst [index]
     

    赋值等操作符前后不能因为对齐而添加多个空格

    # Yes
    x = 1
    y = 2
    long_variable = 3
    
    # No
    x             = 1
    y             = 2
    long_variable = 3

    二元运算符两边放置一个空格

    涉及 =、符合操作符 ( += , -=等)、比较( == , < , > , != , <> , <= , >= , in , not in , is , is not )、布尔( and , or , not )。

    优先级高的运算符或操作符的前后不建议有空格。

    # Yes
    i = i + 1
    submitted += 1
    x = x*2 - 1
    hypot2 = x*x + y*y
    c = (a+b) * (a-b)
    
    # No
    i=i+1
    submitted +=1
    x = x * 2 - 1
    hypot2 = x * x + y * y
    c = (a + b) * (a - b)

    关键字参数和默认值参数的前后不要加空格

    # Yes
    def complex(real, imag=0.0):
        return magic(r=real, i=imag)
    
    # No
    def complex(real, imag = 0.0):
        return magic(r = real, i = imag)

    通常不推荐复合语句(Compound statements: 多条语句写在同一行)。

    # Yes
    if foo == 'blah':
        do_blah_thing()
    do_one()
    do_two()
    do_three()
    
    # No
    if foo == 'blah': do_blah_thing()
    do_one(); do_two(); do_three()

    尽管有时可以在if/for/while 的同一行跟一小段代码,但绝不要跟多个子句,并尽量避免换行。

    # No
    if foo == 'blah': do_blah_thing()
    for x in lst: total += x
    while t < 10: t = delay()
    更不是:
    
    # No
    if foo == 'blah': do_blah_thing()
    else: do_non_blah_thing()
    
    try: something()
    finally: cleanup()
    
    do_one(); do_two(); do_three(long, argument,
                                 list, like, this)
    
    if foo == 'blah': one(); two(); three()

    避免采用的名字

    决不要用字符'l'(小写字母el),'O'(大写字母oh),或 'I'(大写字母eye) 作为单个字符的变量名。一些字体中,这些字符不能与数字1和0区别。用'L' 代替'l'时。

    包和模块名

    模块名要简短,全部用小写字母,可使用下划线以提高可读性。包名和模块名类似,但不推荐使用下划线。

     
  • 相关阅读:
    [C#]StringWriter实现的一个功能
    ASP.NET AJAX入门系列(1):概述
    ASP.NET 2.0 之 Master Page 学习笔记
    C#文件读写常用类介绍
    最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解
    关于Ajax 错误:'sys'未定义解决方法.
    教你解决微软MSN8.5无法安装问题
    对于javascript的function的总结
    ASP.NET开发:在用户控件中添加属性
    如何遍历枚举类型的对象、并获取枚举类型长度
  • 原文地址:https://www.cnblogs.com/venicid/p/7944719.html
Copyright © 2011-2022 走看看