擅长:python、mysql、java
<p><strong>“为什么会返回<code>['yes']</code>”</strong></p>
<p>因为你修改了列表,<code>example</code>。</p>
<p><strong>“即使我没有直接更改变量‘example’。”</strong></p>
<p>但实际上,您为函数提供了由变量<code>example</code>命名的对象。函数使用对象的<code>append</code>方法修改了对象。</p>
<p>正如其他地方所讨论的,<code>append</code>不会产生任何新的东西。它在适当的位置修改一个对象。</p>
<p>见<a href="https://stackoverflow.com/questions/1682567/why-does-pythons-list-append-evaluate-to-false">Why does list.append evaluate to false?</a>,<a href="https://stackoverflow.com/questions/2022031/python-append-vs-operator-on-lists-why-do-these-give-different-results">Python append() vs. + operator on lists, why do these give different results?</a>,<a href="https://stackoverflow.com/questions/1918270/python-lists-append-return-value">Python lists append return value</a>。</p>
<p><strong>如何修改代码,使“example”不受函数的影响?</strong></p>
<p>你说那是什么意思?如果不希望函数更新<code>example</code>,请不要将其传递给函数。</p>
<p>如果希望函数创建新列表,请编写函数以创建新列表。</p>