python - Pass by value or by reference
<p>I have an object with an attribute that is a list. For example:</p>

<pre><code>obj.a = [3, 4, 5]
</code></pre>

<p>I would like to get the following behavior (but I can't manage to find a solution using magics/etc.) :</p>

<pre><code>l = obj.a
obj.a[0] = 2

print(l) --&gt; [3, 4, 5]
print(obj.a) ---&gt; [2, 4, 5]
</code></pre>

<p>Of course I could simply use copy.deepcopy : </p>

<pre><code>l = copy.deepcopy(obj.a) 
</code></pre>

<p>but for several reasons I would like, somehow, to make this step automatic/hide it for my users. </p>

<p>[EDIT]
Using <strong>getattribute</strong> and returning a copy won't work of course:</p>

<pre><code>import copy
class Test:
    def __init__(self):
         self.a = []

    def __getattribute__(self, attr):
        if attr == 'a':
            return copy.deepcopy(super(Test, self).__getattribute__(attr))
</code></pre>

<p>Any help appreciated !</p>

<p>Thnak you,
Thomas </p>

<p>This is not possible without actually cloning the list to another list, you have to use copy() or deepcopy() depending on your requirement.  </p>

python reference