如何給 Python 中多行 if 語句中的每個條件添加註釋?


如果將多行 if 語句的條件用括號括起來,可以直接這麼做。例如:

if (cond1 == 'val1' and
   cond2 == 'val2' and # Some comment
   cond3 == 'val3' and # Some comment
   cond4 == 'val4'):

但是,如果不使用括號,無法這樣做。例如,將生成錯誤的程式碼如下:

if cond1 == 'val1' and \
   cond2 == 'val2' and \ # Some comment
   cond3 == 'val3' and \ # Some comment
   cond4 == 'val4':

最近更新於:2020-06-17

217 次瀏覽

開啟你的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.