0

您好我正在尝试调用当前正在运行的 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(Thread):
   ....
   def increment(self):

class mainPlayer:
   ....
   def run(self, obj):
     self.x = counterTicket()
     self.x.increment()

可能吗?

我只想调用一个已在 mainPlayer 类中调用的运行方法。但我不知道该怎么做。如果我要实例化 counterTicket 类以在 SocketWatcher 类中调用 increment(),它似乎会创建新的 counterTicket 类,而不是 mainPlayer 类当前正在运行的 counterTicket 类

4

2 回答 2

0

我不能完全告诉你在做什么......

也许:

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()
于 2013-09-16T15:57:53.147 回答
0

使 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)
于 2013-09-16T16:22:57.343 回答