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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章