擅长:python、mysql、java
<p>按照惯例,Python的API区分了过程和函数。函数根据其参数(包括任何目标对象)计算新值;过程修改对象,不返回任何内容(即不返回任何内容)。所以过程有副作用,函数没有。update是一个过程,因此它不返回值。</p>
<p>这样做的动机是,否则,你可能会得到不良的副作用。考虑</p>
<pre><code>bar = foo.reverse()
</code></pre>
<p>如果reverse(将列表反转到位)也会返回列表,用户可能会认为reverse返回了一个新的列表,该列表被分配给bar,并且永远不会注意到foo也被修改了。通过使reverse return None,他们立即意识到bar不是反转的结果,并且会更接近于reverse的效果。</p>