Construct a data structure with special requirements in python
<p>Requirements:</p>

<ol>
<li>There is a variable, for example, related_to_dict = 10</li>
<li>Construct a key value pair data, for example, special_dict = {0 :
ref_related_to_dict}</li>
<li>When the variable of related_to_dict changed, the value of
special_dict[0] also changed to the value of related_to_dict
accordingly.</li>
<li>When the value_of special_dict[0], e.g. ref_related_to_dict changed, the
value of related_to_dict also changed to the value of
special_dict[0] accordingly.</li>
</ol>

<p>Is there a way to achieve this task?</p>

<p>You need to wrap the value in some sort of container. </p>

<pre><code>class Ref:
   def __init__(self, v):
        self.val = v
</code></pre>

<p>And then:</p>

<pre><code>related_to_dict = Ref(10)
special_dict = {0: related_to_dict}
</code></pre>

<p>Then it works as desired:</p>

<pre><code>related_to_dict.val = 40
print(special_dict[0].val) # 40
</code></pre>

python data-structures