2016-02-12 54 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