擅长:python、mysql、java
<p>您始终可以用括号扩展<a href="https://docs.python.org/2/reference/lexical_analysis.html#line-structure" rel="noreferrer">logical line across multiple physical lines</a>:</p>
<pre><code>answer = (
'Ten for that? You must be mad!' if does_not_haggle(brian)
else "It's worth ten if it's worth a shekel.")
</code></pre>
<p>这叫做<a href="https://docs.python.org/2/reference/lexical_analysis.html#implicit-line-joining" rel="noreferrer">implicit line joining</a>。</p>
<p>上面使用的是PEP8,每样东西都缩进了一步(称为a<a href="http://legacy.python.org/dev/peps/pep-0008/#indentation" rel="noreferrer">hanging indent</a>)。您还可以缩进额外的行以匹配左括号:</p>
<pre><code>answer = ('Ten for that? You must be mad!' if does_not_haggle(brian)
else "It's worth ten if it's worth a shekel.")
</code></pre>
<p>但这会让你更快地达到80列的最大值。</p>
<p>精确地把<code>if</code>和<code>else</code>部分放在哪里取决于您;我在上面使用了我的个人偏好,但是对于运算符没有任何人同意的特定样式。</p>