<p>只是为了给<a href="https://stackoverflow.com/a/39552503/6793085">@Warren Weckesser</a>答案加上我的5个书呆子硬币。实际上,<code>from numpy import *</code><strong>不会覆盖<code>builtins</code><code>sum</code>函数,它只覆盖</strong><code>__builtins__.sum</code>,因为<code>from ... import *</code>语句将导入模块中定义的所有名称(以下划线开头的名称除外)绑定到当前的<code>global</code>命名空间。并且根据Python的名称解析规则(非官方的<a href="https://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules">LEGB</a>规则),在<code>__builtins__</code>名称空间之前查找<code>global</code>名称空间。因此,如果Python找到所需的名称,在您的例子中是<code>sum</code>,它将返回绑定对象,而不再进一步查看。在</p>
<p><strong>编辑</strong>:
向您展示发生了什么:</p>
<pre><code> In[1]: print(sum, ' from ', sum.__module__) # here you see the standard `sum` function
Out[1]: <built-in function sum> from builtins
In[2]: from numpy import * # from here it is shadowed
print(sum, ' from ', sum.__module__)
Out[2]: <function sum at 0x00000229B30E2730> from numpy.core.fromnumeric
In[3]: del sum # here you restore things back
print(sum, ' from ', sum.__module__)
Out[3]: <built-in function sum> from builtins
</code></pre>
<p><strong><em>第一个注意事项</em></strong>:<code>del</code>不删除对象,它是垃圾收集器的一项任务,它只“取消引用”名称绑定并从当前命名空间中删除名称。在</p>
<p><strong><em>第二个注意事项</em></strong>:内置<code>sum</code>函数的签名是<code>sum(iterable[, start])</code>:</p>
<blockquote>
<p>Sums <code>start</code> and the items of an <code>iterable</code> from left to right and returns the total. <code>start</code> defaults to <code>0</code>. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.</p>
</blockquote>
<p>我您的例子<code>print(sum(range(5),-1)</code>内置的<code>sum</code>求和从-1开始。所以从技术上讲,你的短语<s>在iterable上求和然后从sum</s>中减去第二个args值是不正确的。以后再加上或不重要。但对于列表,它确实是这样的(<em>这个愚蠢的例子只是为了展示这个想法):</p>
^{pr2}$
<p>希望这能澄清你的想法:)</p>