Confused about passing by reference in this Python function
<p>I have this simple Python 2.7 function:</p>

<pre><code>def sort_rows(mat): 
    mat = [sorted(i) for i in mat]
</code></pre>

<p>However, when I run: </p>

<pre><code>M = [[4, 5, 2, 8], [3, 9, 6, 7]]
sort_rows(M)
print(M)
</code></pre>

<p>I get</p>

<pre><code>[[4, 5, 2, 8], [3, 9, 6, 7]]
</code></pre>

<p>Why wasn't <code>M</code> edited? I thought python functions passed lists by reference? Am I missing something?</p>

<p>You could use the return function to return the variable mat at the end of sort row.</p>

<pre><code>return M
</code></pre>

<p>Then if you try- </p>

<pre><code>print(sort_rows(M))
</code></pre>

<p>you should get the result you want. Thanks.</p>

python python-2.7