2016-01-12 40 views
0

我很新的Python,對不起,如果我是一個討厭,但我不斷收到這個錯誤,每當我嘗試我的代碼錯誤如下:Python的ubound方法錯誤

Traceback (most recent call last):<br> 
    File "<pyshell#3>", line 1, in <module> 
    fopen.open_file() 
TypeError:<br> unbound method open_file() must be called with openFile instance as first argument (got nothing instead) 

該計劃的目的是讀入用戶輸入的文件。

這裏是我的代碼:

class openFile: 
    def file_to_open(): 
     fopen = raw_input('Enter the file path : ') 
     open_file = open(fopen) 
     print open_file 
+0

你爲什麼使用類? – timgeb

+0

引發的異常與您的代碼不符。 –

+0

你是怎麼用這個類來創建你的實例的? –

回答

0

我覺得你的意思定義file_to_open作爲類中openFile的成員函數,對不對?如果是這樣,那麼你應該得到的「自我」作爲參數:

def file_to_open(self): 
... 

,你應該如下稱之爲:

f = openFile() 
f.file_to_open() 

這麼說,我想你應該重新考慮使用的這裏有一堂課。相反,你可以定義一個函數

def open_a_file(): 
    filename = raw_input('Enter the file path : ') 
    return open(filename)