现在我需要一个属于另一个班级在一个班级中做某事的属性。如何在Python中访问属性交叉类和交叉文件?
只是想:
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()
-
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()
注意:
时class B
zone_id是不是无,class B
下一个可以做。这意味着,如果class A
zone_id是None,则class B
将等待它。
而class A
和class B
只能初始化一个实例。
class A
和class B
在不同的文件中。
B类可以保存对A类的引用吗? – shuttle87
这是代码味道,但你可以简单地将'w_id'作为类变量来处理,所以'A.w_id'无处不在。 –
@ shuttle87,但'A类'本身需要初始化。并且只有一个实例 – agnewee