In class object, how to auto update attributes when you change only one entry of a list?
<p>I have seen this answer for a very similar question:</p>

<p><a href="http://stackoverflow.com/a/14916491/5726899"><em>In class object, how to auto update attributes?</em></a></p>

<p>I will paste the code here:</p>

<pre><code>class SomeClass(object):
    def __init__(self, n):
        self.list = range(0, n)

    @property
    def list(self):
        return self._list
    @list.setter
    def list(self, val):
        self._list = val
        self._listsquare = [x**2 for x in self._list ]

    @property
    def listsquare(self):
        return self._listsquare
    @listsquare.setter
    def listsquare(self, val):
        self.list = [int(pow(x, 0.5)) for x in val]

&gt;&gt;&gt; c = SomeClass(5)
&gt;&gt;&gt; c.listsquare
[0, 1, 4, 9, 16]
&gt;&gt;&gt; c.list
[0, 1, 2, 3, 4]
&gt;&gt;&gt; c.list = range(0,6)
&gt;&gt;&gt; c.list
[0, 1, 2, 3, 4, 5]
&gt;&gt;&gt; c.listsquare
[0, 1, 4, 9, 16, 25]
&gt;&gt;&gt; c.listsquare = [x**2 for x in range(0,10)]
&gt;&gt;&gt; c.list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</code></pre>

<p>In this code, when I update the list using:</p>

<pre><code>&gt;&gt;&gt; c.list = [1, 2, 3, 4]
</code></pre>

<p>c.listsquare will be updated accordingly:</p>

<pre><code>&gt;&gt;&gt; c.listsquare
[1, 4, 9, 16]
</code></pre>

<p>But when I try:</p>

<pre><code>&gt;&gt;&gt; c.list[0] = 5
&gt;&gt;&gt; c.list
[5, 2, 3, 4]
</code></pre>

<p>Listsquares is not updated:</p>

<pre><code>&gt;&gt;&gt; c.listsquare
[1, 4, 9, 16]
</code></pre>

<p>How can I make listsquare auto update when I change only one item inside the list?</p>

<p>One way you could do it is by having a private helper <code>_List</code> class which is almost exactly like the built-in <code>list</code> class, but also has a <code>owner</code> attribute. Every time one of a <code>_List</code> instance's elements is assigned a value, it can then modify the <code>listsquare</code> property of its <code>owner</code> to keep it up-to-date. Since it's only used by <code>SomeClass</code> it can be nested inside of it to provide even more encapsulation.</p>

<pre><code>class SomeClass(object):
    class _List(list):
        def __init__(self, owner, *args, **kwargs):
            super(SomeClass._List, self).__init__(*args, **kwargs)
            self.owner = owner

        def __setitem__(self, index, value):
            super(SomeClass._List, self).__setitem__(index, value)
            self.owner.listsquare[index] = int(value**2)

    def __init__(self, n):
        self.list = SomeClass._List(self, range(0, n))

    @property
    def list(self):
        return self._list
    @list.setter
    def list(self, val):
        self._list = val
        self._listsquare = [x**2 for x in self._list ]

    @property
    def listsquare(self):
        return self._listsquare
    @listsquare.setter
    def listsquare(self, val):
        self.list = [int(pow(x, 0.5)) for x in val]

c = SomeClass(5)
print(c.list)        # --&gt; [0, 1, 2, 3, 4]
print(c.listsquare)  # --&gt; [0, 1, 4, 9, 16]
c.list[0] = 5
print(c.list)        # --&gt; [5, 1, 2, 3, 4]
print(c.listsquare)  # --&gt; [25, 1, 4, 9, 16]
</code></pre>

python class attributes