如何在 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 年 6 月 17 日

217 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.