2009-06-23 55 views
3

如何在python中調用涉及自我和參數的調用?在python中剖析自我和參數?

def performProfile(self): 
    import cProfile 
    self.profileCommand(1000000) 

def profileCommand(self, a): 
    for i in a: 
     pass 

在上面的例子中,我將如何描述對profileCommand的調用?我想出了我需要用runctx作爲參數,但我該如何處理自我? (代碼實際上涉及一個用戶界面,因此很難將呼叫分開發送到配置文件)。

回答

11

你需要通過局部/全局詞典並傳遞第一個參數你通常會輸入的內容是

cProfile.runctx("self.profileCommand(100)", globals(),locals()) 

使用這樣的事情

class A(object): 
    def performProfile(self): 
     import cProfile 
     cProfile.runctx("self.profileCommand(100)", globals(),locals()) 

    def profileCommand(self, a): 
     for i in xrange(a): 
      pass 
     print "end." 

A().performProfile() 

,不調用輪廓整個UI,配置文件中的特定功能或計算