python bad operand type for unary -: 'NoneType'
<p>I send you a question because I have a problem on python and I don't understand why.
I created a function "mut1" to change the number inside a list (with a probability to 1/2) either in adding 1 or subtracting 1, except for 0 and 9:</p>

<pre><code>def mut1 (m):
    i=np.random.randint(1,3)
    j=np.random.randint(1,3)
    if i==1:
        if 0&lt;m&lt;9:
            if j==1:
                m=m+1
            elif j==2:
                m=m-1
        elif m==0:
            if j==1:
                m=1
            if j==2:
                m=9
        elif m==9:
            if j==1:
                m=0
            if j==2:
                m=8
    print m
</code></pre>

<p>mut1 function well, for example, if I create a list P1:</p>

<pre><code>&gt;&gt;&gt;p1=np.array(range(8),int).reshape((4, 2))
</code></pre>

<p>After that, I apply "mut1" at a number (here 3) in the list p1</p>

<pre><code>&gt;&gt;&gt;mut1(p1[1,1]) 
</code></pre>

<p>Hovewer if I write:</p>

<pre><code>&gt;&gt;&gt; p1[1,1]=mut1(p1[1,1])
</code></pre>

<p>I have a message error:</p>

<blockquote>
  <p>Traceback (most recent call last):   File "&lt;stdin&gt;", line 1, in
  &lt;module&gt; TypeError: long() argument must be a string or a number, not
  'NoneType'</p>
</blockquote>

<p>That happens because you have to make your <code>mut1</code> return an <code>numpy.int64</code> type of result. So I tried with the following modified code of yours and worked.</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; import random
&gt;&gt;&gt;
&gt;&gt;&gt; def mut1 (m):
...     i=np.random.randint(1,3)
...     j=np.random.randint(1,3)
...     if i==1:
...         if 0&lt;m&lt;9:
...             if j==1:
...                 m=m+1
...             elif j==2:
...                 m=m-1
...         elif m==0:
...             if j==1:
...                 m=1
...             if j==2:
...                 m=9
...         elif m==9:
...             if j==1:
...                 m=0
...             if j==2:
...                 m=8
...     return np.int64(m)
...
&gt;&gt;&gt; p1=np.array(range(8),int).reshape((4, 2))
&gt;&gt;&gt; mut1(p1[1,1])
2
&gt;&gt;&gt; p1[1,1]=mut1(p1[1,1])
&gt;&gt;&gt;
</code></pre>

<p>So the only thing you need to change is to replace <code>print m</code> with <code>return np.int64(m)</code> and then should work!</p>

<p>You will easily understand why this happened with the following kind of debugging code:</p>

<pre><code>&gt;&gt;&gt; type(p1[1,1])
&lt;type 'numpy.int64'&gt;
&gt;&gt;&gt; type(mut1(p1[1,1]))
&lt;type 'NoneType'&gt;
</code></pre>

python nonetype