2016-02-12 53 views
1

我很抱歉,但我剛開始使用Python,但我在下面的代碼未解決的參考get_func()錯誤:懸而未決參考get_func()在pycharm

class Foo: 

    fo = open(file_name, "r") 

    with open(file_name, 'r') as file: 

     examples = int(file.readline()) 
     attributes = int(file.readline()) 

     name_of_attributes = [n for n in (file.readline().replace(" ", "")).split(",")] 

     all_examples = file.readlines() 



    get_func(); // error here 

    def get_func(self): 
     list_of_keys = ['S_Length', 'S_Width', 'P_Length', 'P_Width', 'Predicate'] 
     with open('example.txt') as f: 
      for line in f: 
       return; 
+2

正是在這一範圍內未解決的參考。名稱'get_func'在那裏不存在。我會建議閱讀基本的Python教程,因爲這段代碼展現了一些基本的錯誤,並且回答現在定義的這個問題是毫無意義的。 –

回答

1

在那裏你點調用該函數,Python尚未遇到它的定義,所以它還不知道你所指的是什麼。在C/C++中(根據代碼中的註釋來判斷),編譯代碼然後運行代碼有明顯的區別。在Python中,解釋器在概念上只是解釋它(有字節碼編譯,但除此之外)。

嘗試更改順序,以便調用的是定義後:

def get_func(self): # first define 
    ... 

get_func() # now invoke it 
0

要訪問你應該使用self屬性您在方法get_func聲明已設置,這樣self.get_func()一個類的方法。最後可以放棄冒號。

我想你應該閱讀更多關於Python類,看看這個維基教科書的文章 - Python beginner's tutorial to classes