2013-09-16 49 views
0

您好我正在嘗試調用當前正在運行的self.x.run()。此代碼已安排,不能更改訂購如何在python中調用當前正在運行的方法/類

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類

+3

你能澄清一下嗎?這並不明顯,你期待什麼。 – FatalError

+0

你有沒有試過在你的'def run'開頭設置一個全局變量等於'self'或'self.x'的(顯而易見的)解決方案,然後在函數返回之前將該變量設置爲'None'?我相當肯定你可以用你的其他線程以這種方式拉取當前運行的方法/類實例的引用。 – Shashank

回答

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() 
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) 
相關問題