<h2>PEP8兼容解决方案:格式化行</h2>
<p>实际上<a href="http://www.python.org/dev/peps/pep-0008/" rel="noreferrer">PEP8</a>说:</p>
<blockquote>
<p>Long lines can be
broken over multiple lines by wrapping expressions in <strong>parentheses. These
should be used in preference to using a backslash for line continuation</strong>.
Make sure to indent the continued line appropriately.</p>
</blockquote>
<p>所以我想你的代码应该是这样的:</p>
<pre><code>(
myOjbect
.doStuf()
.doMoreStuf()
.goRed()
.goBlue()
.die()
)
</code></pre>
<h2>替代解决方案:拆分成单独的语句</h2>
<p>从语法上看,对于每个方法调用返回的值,有两个可能的选项:</p>
<ol>
<li>每个方法(可能除了<code>die()</code>,这不是必需的,因为它的结果没有被使用)都返回修改过的实例(调用它的同一个实例)。在</li>
<li>每个方法(仍然,<code>die()</code>不需要这样做)返回调用它的实例的<em>copy</em>。在</li>
</ol>
<h2>可变对象的解决方案(方法返回原始实例)</h2>
<p>在<strong>第一种情况下</strong>(返回同一个实例时),将较长的行拆分为多个语句的解决方案是:</p>
^{pr2}$
<p>真实世界的例子涉及可变对象:</p>
<pre><code>my_stuff = []
my_stuff.append('laptop') # my_stuff == ['laptop']
my_stuff.append('jacket') # my_stuff == ['laptop', 'jacket']
my_stuff.append('apple') # my_stuff == ['laptop', 'jacket', 'apple']
</code></pre>
<p>(尽管<code>list.append()</code>不返回任何内容,只是为了保持一致性并明确声明它是可变的)</p>
<h2>不可变对象的解决方案(方法返回修改后的副本)</h2>
<p>在<strong>第二种情况下</strong>(返回copy时),执行类似操作的解决方案是:</p>
<pre><code>myObject = myObject.doStuf()
myObject = myObject.doMoreStuf()
myObject = myObject.goRed()
myObject = myObject.goBlue()
myObject.die()
</code></pre>
<p>真实世界的例子涉及不可变对象:</p>
<pre><code>name = '-Tadek-'
name = name.strip('-') # name == 'Tadek'
name = name.lower() # name == 'tadek'
name = name.replace('k', 'ck') # name == 'tadeck'
</code></pre>