第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在“ if”語句中設(shè)置多行條件的樣式?

在“ if”語句中設(shè)置多行條件的樣式?

慕田峪9158850 2019-11-04 15:31:44
有時我將ifs中的長條條件分解為幾行。最明顯的方法是:  if (cond1 == 'val1' and cond2 == 'val2' and      cond3 == 'val3' and cond4 == 'val4'):      do_something在視覺上不是很吸引人,因為動作與條件融為一體。但是,這是使用正確的4個空格的Python縮進的自然方法。目前,我正在使用:  if (    cond1 == 'val1' and cond2 == 'val2' and          cond3 == 'val3' and cond4 == 'val4'):      do_something但這不是很漂亮。:-)您能推薦一種替代方法嗎?
查看完整描述

3 回答

?
胡說叔叔

TA貢獻1804條經(jīng)驗 獲得超8個贊

您不需要在第二條條件行上使用4個空格??赡苁褂茫?/p>


if (cond1 == 'val1' and cond2 == 'val2' and 

       cond3 == 'val3' and cond4 == 'val4'):

    do_something

另外,不要忘記空白比您想象的更靈活:


if (   

       cond1 == 'val1' and cond2 == 'val2' and 

       cond3 == 'val3' and cond4 == 'val4'

   ):

    do_something

if    (cond1 == 'val1' and cond2 == 'val2' and 

       cond3 == 'val3' and cond4 == 'val4'):

    do_something

兩者都相當(dāng)丑陋。


也許丟了括號(盡管《風(fēng)格指南》不鼓勵這樣做)?


if cond1 == 'val1' and cond2 == 'val2' and \

   cond3 == 'val3' and cond4 == 'val4':

    do_something

這至少使您與眾不同。


甚至:


if cond1 == 'val1' and cond2 == 'val2' and \

                       cond3 == 'val3' and \

                       cond4 == 'val4':

    do_something

我想我更喜歡:


if cond1 == 'val1' and \

   cond2 == 'val2' and \

   cond3 == 'val3' and \

   cond4 == 'val4':

    do_something

這是《樣式指南》,(自2010年起)建議使用括號。


查看完整回答
反對 回復(fù) 2019-11-04
?
陪伴而非守候

TA貢獻1757條經(jīng)驗 獲得超8個贊

在簡并的情況下,我采用了以下內(nèi)容:簡而言之,其為AND或OR。


if all( [cond1 == 'val1', cond2 == 'val2', cond3 == 'val3', cond4 == 'val4'] ):


if any( [cond1 == 'val1', cond2 == 'val2', cond3 == 'val3', cond4 == 'val4'] ):

它可以刮掉幾個字符,并清楚表明該條件沒有任何微妙之處。


查看完整回答
反對 回復(fù) 2019-11-04
?
汪汪一只貓

TA貢獻1898條經(jīng)驗 獲得超8個贊

有人必須在這里提倡使用垂直空格!:)


if (     cond1 == val1

     and cond2 == val2

     and cond3 == val3

   ):

    do_stuff()

這使得每個條件都清晰可見。它還可以更清晰地表達更復(fù)雜的條件:


if (    cond1 == val1

     or 

        (     cond2_1 == val2_1

          and cond2_2 >= val2_2

          and cond2_3 != bad2_3

        )

   ):

    do_more_stuff()

是的,為了清楚起見,我們將權(quán)衡一些垂直房地產(chǎn)。海事組織值得。


查看完整回答
反對 回復(fù) 2019-11-04
  • 3 回答
  • 0 關(guān)注
  • 489 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號