zoukankan      html  css  js  c++  java
  • Python编码规范04-基础规范--空格

    1、  在二元运算符两边各空一格[=,-,+=,==,>,in,is not, and]:

    # 正确的写法
    i = i + 1
    submitted += 1
    x = x * 2 - 1
    hypot2 = x * x + y * y
    c = (a + b) * (a - b)
    
    # 不推荐的写法
    i=i+1
    submitted +=1
    x = x*2 - 1
    hypot2 = x*x + y*y
    c = (a+b) * (a-b)

    2、  函数的参数列表中,,之后要有空格

    # 正确的写法
    def complex(real, imag):
        pass
    
    # 不推荐的写法
    def complex(real,imag):
        pass

    3、  函数的参数列表中,默认值等号两边不要添加空格

    # 正确的写法
    spam(ham[1], {eggs: 2})
    
    # 不推荐的写法
    spam( ham[1], { eggs : 2 } )

    4、  左括号之后,右括号之前不要加多余的空格

    # 正确的写法
    spam(ham[1], {eggs: 2})
    
    # 不推荐的写法
    spam( ham[1], { eggs : 2 } )

    5、  字典对象的左括号之前不要多余的空格

    # 正确的写法
    dict['key'] = list[index]
    
    # 不推荐的写法
    dict ['key'] = list [index]

    6、  不要为对齐赋值语句而使用的额外空格

    # 正确的写法
    x = 1
    y = 2
    long_variable = 3
    
    # 不推荐的写法
    x             = 1
    y             = 2
    long_variable = 3

    7、  不要在逗号, 分号, 冒号前面加空格, 但应该在它们后面加(除了在行尾)

    Yes: if x == 4:
             print x, y
         x, y = y, x
    No:  if x == 4 :
             print x , y
         x , y = y , x
  • 相关阅读:
    操作技巧——电脑远程控制
    软件安装——internal error2503/2502
    系统常识——虚拟内存地址
    操作技巧——保障无线上网的技巧
    操作技巧——输入法转换问题
    软件安装——彻底卸载MySQL
    Java——this
    python百度贴吧爬虫
    糗事百科python爬虫
    python请求带cookie
  • 原文地址:https://www.cnblogs.com/mazhiyong/p/12504690.html
Copyright © 2011-2022 走看看