2012-12-12 42 views
0

我想從main..how中調用一個類中定義的函數,我可以這樣做嗎?如何在主類中調用一個類的函數

class file_process: 
    def findb(self, id): 
    .... 

def main(): 
    id = sys.argv[2] 
file_process.findb(id)//how to call findb function from main,this is giving an error 
+0

首先,您需要仔細研究這些類和蟒蛇。您正在調用*方法*,並且想要在類實例上調用方法。 –

+0

在學習了更多的OOP之後,您可能會決定要使該函數成爲靜態方法:http://stackoverflow.com/questions/735975/static-methods-in-python –

回答

2

由於finddb方法,你需要調用它的實例file_process

file_process().finddb(id) 

我強烈建議你學習了Python和課程,我可以推薦在您繼續之前,您可以使用Python tutorial

+0

獲取錯誤... TypeError:__init __()只需要2個參數(1給出),它應該是file_process()。finddb(self,id) – user1795998

+0

@ user1795998:查找'__init__'函數並查看期望的結果。您需要將* something *傳遞給'file_process()'類,但是我們無法猜測沒有更多細節的內容。 –

+0

雅..我讀了教程,並找出了..謝謝 – user1795998

1

您需要先創建類的實例:

process = file_process() 
process.findb(id) 
相關問題