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><em>"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."</em></p>

<p>You have basically answered your own question!</p>

<p>It is not technically correct to say that the behavior of the function has been <em>modified</em>.  <code>from numpy import *</code> results in "shadowing" the <a href="https://docs.python.org/2/library/functions.html#sum" rel="nofollow">builtin <code>sum</code> function</a> with the <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html" rel="nofollow">numpy <code>sum</code> function</a>, so when you use the name <code>sum</code>, Python finds the numpy version instead of the builtin version (see @godaygo's answer for more details).  These are <em>different</em> functions, with different arguments.  It is generally a bad idea to use <code>from somelib import *</code>, for exactly this reason.  Instead, use <code>import numpy as np</code>, and then use <code>np.sum</code> when you want the numpy function, and plain <code>sum</code> when you want the Python builtin function.</p>

python numpy sum