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
  • 相关阅读:
    Luogu P4053 [JSOI2007]建筑抢修
    CF894E Ralph and Mushrooms
    Boruvka
    Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland
    HDU 3714/UVA1476 Error Curves
    HDU 5410 CRB and His Birthday
    HDU 1796 How many integers can you find
    UVA 11624 Fire!
    POJ 3279 Dungeon Master
    POJ 1321 棋盘问题
  • 原文地址:https://www.cnblogs.com/mazhiyong/p/12504690.html
Copyright © 2011-2022 走看看