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>Use a class (maybe a bit overkill):</p>

<pre><code>class Foo:
    def __init__(self):
        a = 0

def bar(f):
    f.a = 2

foo = Foo()
foo.a = 1
bar(foo)
print(foo.a)
</code></pre>

python pass-by-reference