How to access property cross class and cross file in Python?
<p>Now I need a property which in another class to do something in one class.</p>

<p>just like:</p>

<pre><code>a.py

class A:
    def __init__(self, io_loop):         # the same io_loop instance 
        self.access = None
        self.w_id = None
        self.io_loop = io_loop

    @gen.coroutine
    def setup(self):
        # `async_client` has the `get`, 'post', 'put', 'delete' methods 
        self.access = yield async_client()

    @gen.coroutine
    def do_something(self):
        self.w_id = self.access.get('w_id')
        ...

    def run(self):
        self.io_loop.run_sync(self.setup)
        self.io_loop.spawn_callback(self.do_something)
        self.io_loop.start()

if __name__ == '__main__':
    a = A()
    a.run()
</code></pre>

<p>-</p>

<pre><code>b.py

class B:
    def __init__(self, io_loop):
        self.w_id = None
        self.io_loop = io_loop           # the same io_loop instance    

    # How can i get the w_id from `class A`     

    def run(self):
        ... 

if __name__ == '__main__':
    b = B()
    b.run() 
</code></pre>

<p><strong>Notice</strong>:</p>

<p>when zone_id of <code>class B</code> is not None, <code>class B</code> can do next. that's means, if <code>class A</code> zone_id is None, <code>class B</code> will waiting for it.</p>

<p>And the <code>class A</code> and <code>class B</code> only could initialize one instance.</p>

<p><strong>the <code>class A</code> and <code>class B</code> in differents files.</strong></p>

<p>You can't access that variable until you create an instance that initializes. Otherwise, <code>w_id</code> doesn't exist in <code>A</code>.</p>

<p>If you want to give <code>w_id</code> an arbitrary value for access from other classes, put it as a class variable, means you write directly <code>w_id = 'some value'</code> inside class <code>A</code> with the same indentation as the <code>def</code>s:</p>

<pre><code>class A:
    w_id = something
    def __init__(self):
        ...
class B:
    def __init__(self):
        self.w_id = A.w_id
</code></pre>

<p>Otherwise, you need an instance of <code>A</code>, like that:</p>

<pre><code>class B:
    def __init__(self):
        a = A()
        a.do_something()
        self.w_id = a.w_id
</code></pre>

<p>The only other option is to create the same functions inside <code>B</code>:</p>

<pre><code>class B:
    ...
    @gen.coroutine 
    def setup(self): 
        # `async_client` has the `get`, 'post', 'put', 'delete' methods
        self.access = yield async_client()   
    @gen.coroutine 
    def do_something(self): 
        self.w_id = self.access.get('w_id') 
        ...
</code></pre>

<p>As you mentioned that io_loop is the same instance in all of the classes, it might occur that you need to create a copy of it if your functions uses it. You can't change a variable and expect it to stay unchanged. </p>

python tornado