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>Inheritance does not work this way in Python. However, I think your problem is mutability rather than inheritance itself. (You did not apply inheritance, I said these because you used it as a tag and in the title.)</p>

<p>Try it like this.</p>

<pre><code>class a():
    def __init__(self):
        self.x = [5]

class b():
    def __init__(self, r):
        self.y = r
    def chg(self, a):
        self.y = a

m = a()
n = b(m.x)
n.y[0] = 99
print m.x
# Gives 99
</code></pre>

<p>This way, you only create one list, and use it with two different classes.</p>

<p><strong>Note:</strong> You can think this as the <em>pass by reference</em> property in C-type languages.</p>

<p>Just to keep in mind, every assignment in Python is <em>pass by reference</em> because Python handles variables different than those languages. <a href="http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference">Read more about it.</a> (This does not affect mutability.)</p>

<p><strong>Edit:</strong> I see you edited you question. Now let me explain why your code does not work as you expected.</p>

<p>Mutability means you can change the elements inside a container, without assigning it to a new address in memory.</p>

<p>When you do,</p>

<pre><code>n.y = ["c", "d"]
</code></pre>

<p>You do not change the list,(So you do not <em>mutate</em> the list) you just assign a new list to that variable name. To change the original list, you must alter every element by using <code>list[index]</code>, so the element would be changed, but the list will be the same list. So you can do this</p>

<pre><code>m = a()
n = b(m.x)

new_list = ["c", "d"]

for i, elem in enumerate(new_list):
    # This way you change every element one-by-one.
    n.y[i] = elem

print m.x
</code></pre>

<p>If you are not familiar with <code>enumerate</code>, study it immediately. But if you want to use simpler code(And more similar to C implementation.) You can make the assignments like this,</p>

<pre><code>for i in range(len(new_list)):
    n.y[i] = new_list[i]
</code></pre>

python class inheritance properties