如何在python中调用当前正在运行的方法/类
您好,我正在尝试调用当前正在运行的self.x.run()。此代码已安排,订购时无法更改如何在python中调用当前正在运行的方法/类,python,class,Python,Class,您好,我正在尝试调用当前正在运行的self.x.run()。此代码已安排,订购时无法更改 class SocketWatcher(Thread): ..... def checker(self): # here I want to call the currently running self.x.run() that has been # created in the mainPlayer class.. class counterTicket(Threa
class SocketWatcher(Thread):
.....
def checker(self):
# here I want to call the currently running self.x.run() that has been
# created in the mainPlayer class..
class counterTicket(Thread):
....
def increment(self):
class mainPlayer:
....
def run(self, obj):
self.x = counterTicket()
self.x.increment()
可能吗
我只想调用一个在mainPlayer类中调用的正在运行的方法。但是我不知道怎么做。如果我要实例化counterTicket类来调用SocketWatcher类的increment(),它似乎会创建新的counterTicket类,而不是mainPlayer类当前正在运行的counterTicket类我不太清楚您在做什么 也许:
class mainPlayer(object):
def __init__(self, *args):
self.ticket = counterTicket()
def run(self):
self.ticket.increment()
class SocketWatcher(Thread):
def checker(self, player):
player.run()
使run()成为类方法:
class mainPlayer(object):
# whatever methods predefined
@classmethod
def run(cls, obj):
if not isinstance(cls, obj):
raise TypeError("input type error")
input.x = counterTicket()
input.x.increment()
然后可以在实例中调用它:
# a method in class definition
def foo(self, input):
mainPlayer.run(input)
你能澄清一下吗?您的期望并不明显。您是否尝试过(明显的)解决方案,在
def运行开始时将全局变量设置为self
或self.x
,然后在函数返回之前将该变量设置为None
?我很确定,您可以通过其他线程以这种方式获取当前正在运行的方法/类实例的引用。