Python sum() has a different result after importing numpy
<p>I came across this problem by Jake VanderPlas and I am not sure if my understanding of why the result differs after importing the numpy module is entirely correct.</p>

<pre><code>&gt;&gt;print(sum(range(5),-1)
&gt;&gt; 9
&gt;&gt; from numpy import *
&gt;&gt; print(sum(range(5),-1))
&gt;&gt; 10
</code></pre>

<p>It seems like in the first scenario the sum function calculates the sum over the iterable and then subtracts the second args value from the sum.</p>

<p>In the second scenario, after importing numpy, the behavior of the function seems to have modified as the second arg is used to specify the axis along which the sum should be performed.</p>

<p>Exercise number (24)
Source - <a href="http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html" rel="nofollow">http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html</a> </p>

<p>Only to add my 5 pedantic coins to <a href="http://stackoverflow.com/a/39552503/6793085">@Warren Weckesser</a> answer. Really <code>from numpy import *</code> <strong>does not overwrite</strong> the <code>builtins</code> <code>sum</code> function, it only <strong>shadows</strong> <code>__builtins__.sum</code>, because <code>from ... import *</code> statement binds all names defined in the imported module, except those beginning with an underscore, to your current <code>global</code> namespace. And according to Python's name resolution rule (unofficialy <a href="http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules">LEGB</a> rule), the <code>global</code> namespace is looked up before <code>__builtins__</code> namespace. So if Python finds desired name, in your case <code>sum</code>, it returns you the binded object and does not look further.</p>

<p><strong>EDIT</strong>:
To show you what is going on:</p>

<pre><code> In[1]: print(sum, ' from ', sum.__module__)    # here you see the standard `sum` function
Out[1]: &lt;built-in function sum&gt;  from  builtins

 In[2]: from numpy import *                     # from here it is shadowed
        print(sum, ' from ', sum.__module__)
Out[2]: &lt;function sum at 0x00000229B30E2730&gt;  from  numpy.core.fromnumeric

 In[3]: del sum                                 # here you restore things back
        print(sum, ' from ', sum.__module__)
Out[3]: &lt;built-in function sum&gt;  from  builtins
</code></pre>

<p><strong><em>First note</em></strong>: <code>del</code> does not delete objects, it is a task of garbage collector, it only "dereference" the name-bindings and delete names from current namespace. </p>

<p><strong><em>Second note</em></strong>: the signature of built-in <code>sum</code> function is <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>I your case <code>print(sum(range(5),-1)</code> for built-in <code>sum</code> summation starts with -1. So technically, your phrase <s>the sum over the iterable and then subtracts the second args value from the sum</s> isn't correct. For numbers it's really does not matter to <em>start with</em> or <em>add/subtract</em> later. But for lists it does (<em>silly example only to show the idea</em>):</p>

<pre><code> In[1]: sum([[1], [2], [3]], [4])
Out[1]: [4, 1, 2, 3]               # not [1, 2, 3, 4]
</code></pre>

<p>Hope this will clarify your thoughts :)</p>

python numpy sum