Python 3: How do I get one class to modify values in an instance of another class?
<p>I am learning to code in Python and am writing a short program with three classes as practice. The first class uses instances of the second and third classes as attributes. I am trying to get a method in the third class change a value in an instance of itself as well as in an instance of the second class.
So far I have something like this:</p>

<pre><code>Class_a():
    self.b_attribute = Class_b()
    self.c_attribute = Class_c()

Class_b():
    self.b_value = 5

Class_c():
    self.c_value = 1

    def change_values():
        self.c_value += 1 

instance = Class_a()
instance.c_attribute.change_values()
</code></pre>

<p>Calling <code>instance.c_attribute.change_values()</code> increases <code>c_value</code> for this instance of <code>Class_a</code> by 1. Is it possible to write <code>change_values()</code> such that calling <code>instance.c_attribute.change_values()</code> also changes of <code>b_value</code>
this instance of <code>Class_a</code>?
If it is possible how would I write that code?</p>

<p>You have a hierarchy on classes of this
Class_a
|         |
Class_b Class_c</p>

<p>now you have done making changes in Class_c and it affects Class_a with regard to the c_attribute component.</p>

<p>This technique is very advanced, and I am surprised you learn it as a beginner.</p>

<p>However, this is accessible only because c is part of a. But notice c is not part of b, and vice versa. See the hierarchy that b and c have no relationship.</p>

<p>A clear way to handle it is to make a general function in a.</p>

<p>But if you want to make a change in attribute_c so that it has a chain effect on attribute_b, you have to set up a trigger in a that can be called by c.</p>

<p>e.g</p>

<pre><code>class a():
   def a_change_b(): #trigger function
      self.b_attribute += 1

class c():
   def change_b():
     a_change_b()  #try to call the trigger function outside c but inside a
</code></pre>

<p>Hope it works.</p>

python class attributes