How can we simulate pass by reference in python?
<p>Let's say we have a function <code>foo()</code></p>

<pre><code>def foo():
  foo.a = 2

foo.a = 1
foo()

&gt;&gt; foo.a
&gt;&gt; 2
</code></pre>

<p>Is this pythonic or should I wrap the variable in mutable objects such as a list?</p>

<p>Eg:</p>

<pre><code>a = [1]

def foo(a):
  a[0] = 2
foo()

&gt;&gt; a

&gt;&gt; 2
</code></pre>

<p>Since you "want to mutate the variable so that the changes are effected in global scope as well" use the <code>global</code> keyword to tell your function that the name <code>a</code> is a global variable.  This means that any assignment to <code>a</code> inside of your function affects the global scope.  Without the <code>global</code> declaration assignment to <code>a</code> in your function would create a new local variable.</p>

<pre><code>&gt;&gt;&gt; a = 0
&gt;&gt;&gt; def foo():
...     global a
...     a = 1
...
&gt;&gt;&gt; foo()
&gt;&gt;&gt; a
1
</code></pre>

python pass-by-reference