Zen of Python で、Tim Peters は次のように述べていFlat is better than nested.
ます。私がそれを正しく理解していれば、Python では次のようになります。
<statement-1> if <condition> else <statement-2>
一般に、これよりも優先されます。
if <condition>:
<statement-1>
else:
<statement-2>
ただし、他の言語では、三項演算子をネストしないように言われ、代わりに従来のif...else
. 私の質問は、これを使用する必要があるかどうかです。
(<statement-1> if <condition-1> else <statement-2>) if <condition-2> else <statement-3>
また
if <condition-2>:
if <condition-1>:
<statement-1>
else:
<statement-2>
else:
<statement-3>
? 特にステートメントと条件が長く、最初の行を分割する必要がある場合は?