python编码规范(三)--空行,换行,缩进

原文链接:https://www.jianshu.com/p/567bbc940247

1.空行

  • 空一行:用于类成员函数之间,或者用于区分不同逻辑块
  • 空两行:类与类,类与函数,函数与函数之间
class Test(object):
    """Test class,提供通用的方法"""
    def __init__(self):
        """Test的构造器:"""
        pass

    def function1(self):
        pass

    def function2(self):
        pass


def function3():
    pass

2.换行

非集合元素

  • 反斜杠
with open('test.txt','w') as file_1, \
     open("test2.txt", 'w') as file_2:
    file_2.write(file_1.read())
  • 字符串
query_str = ('my name'
             'is'
             ' %s') % "Tom"
print query_str
  • 二元运算符
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

3.缩进

集合元素

  • 没有子模块: 一层缩进
my_list = [
    1, 2, 3,
    4, 5, 6,
    ]

result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
    )
  • 有子模块: 两层缩进
#if and 
if (this_is_one_thing
        and that_is_another_thing):
    do_something()
# function
def long_function_name(
        var_one, var_two, 
        var_three,var_four):
    print(var_one)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章