Class property inheritance when property is another class' property in python
<p>I have a class:</p>

<pre><code>class a():
    def __init__(self):
        self.x = ["a", "b"]
</code></pre>

<p>and another class:</p>

<pre><code>class b():
    def __init__(self, r):
        self.y = r
    def chg(self, a):
        self.y = a
</code></pre>

<p>I do:</p>

<pre><code>&gt;&gt;&gt; m = a()
&gt;&gt;&gt; m.x
["a", "b"]
&gt;&gt;&gt; n = b(m.x)
&gt;&gt;&gt; n.y
["a", "b"]
&gt;&gt;&gt; n.y = ["c", "d"]
&gt;&gt;&gt; n.y
["c", "d"]
&gt;&gt;&gt; m.x
["a", "b"]
</code></pre>

<p>Now why didn't <code>m.x</code> change to <code>["c", "d"]</code>? How can I achieve this?</p>

<p>Oke so you need to do a little reading up on how memory works in python. What you do here:</p>

<pre><code>&gt;&gt;&gt; n = b(m.x)
&gt;&gt;&gt; n.y
5
</code></pre>

<p>Is asking for the value m.x en sending it to the constructor of n. The value of m.x is 5 at that moment so 5 is passed to the constructor which then saves the NUMBER 5 in the object not the memory address to the variable m.x. Meaning that n is not linked in anyway to m. So if you where to update m.x, n would remain the same because the data for m is stored in a completely different place than that of n.</p>

<p>In a language like C you could pass the address of the variable to the constructor but this is not possible in Python. The only variables in python that use addresses are array's. So you might want to look into using single element array's if you really need this functionality. </p>

<p>Another tip I would give you is give your classes and variables proper names not just single character names!</p>

python class inheritance properties