python編碼規范(二)——空行,換行,縮進


python編碼規范(二)——空行,換行,縮進

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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM