How to get the reference of one variable of one class in another class in python?
<p>I have a class C1, and another class C2 which takes one instance of C1 as a variable. If I want to get the variable of C1 in C2, I have to use <code>self.c1.variable</code>. How can I get a reference of the variable of C1 in C2 so that I can get it directly?</p>

<pre><code>class C1():
    def __init__(self,a):
        self.variable = a

class C2():
    def __init__(self, c1):
        self.c1 = c1

    def print_variable(self):
        print self.c1.variable


c1 = C1(1)
c2 = C2(c1)
c2.print_variable()
</code></pre>

<p>You can use <code>@property</code> decorator to achieve that:</p>

<pre><code>class C2():
    def __init__(self, c1):
        self.c1 = c1

    @property
    def variable(self):
        return self.c1.variable
</code></pre>

<p>In case you want to modify the <code>variable</code> in C2 instance:</p>

<pre><code>    @variable.setter
    def variable(self, value):
        self.c1.variable = value
</code></pre>

python class reference