我正在尝试访问一个类的实例(称为“ a”),该实例传递给第二个类(称为“ b”)。我的意图是通过装饰实例b的类来访问实例“ a”,以便我可以在“ a”上设置一些后台线程任务,同时仍将“ b”用于其他更重要的任务。这可能吗?
import threading
import inspect
Class doStuff():
def __init__(self, somePropertyFromAnotherClass):
self.lock = threading.Lock()
self.prop = somePropertyFromAnotherClass
def doCoolThreadingStuff():
print("do threading stuff with {}".format(self.prop))
def someDecorator(cls):
def wrapper(cls):
print(inspect.getrgspec(cls.__init__))
#ds = doStuff() ## this is the bit that i can't figure out!
wrapper(cls)
return cls
Class A():
def __init__(self):
self.obj = "i'm an object"
@someDecorator
Class B():
def __init__(self, obj):
self.obj = obj
def doSomethingWithObj():
print('doing something with obj')
if __name__ == "__main__":
a = A()
b = B(a)
答案 0 :(得分:0)
装饰器的格式不正确
def someDecorator(cls_obj):
def wrapper(*args):
ds = doStuff(args[0]) # positional : /
ds.doCoolThreadingStuff()
return cls_obj(*args)
return wrapper
我能够使用上面的装饰器定义来访问A类(传递给B类)的实例