很抱歉,我刚刚开始使用 python,但我在以下代码中遇到了未解析的引用 get_func() 错误:
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;
最佳答案
当你调用这个函数时,Python 还没有遇到它的定义,所以它还不知道你所指的是什么。在 C/C++ 中(从代码内注释来看),编译代码和运行代码之间有明显的区别。在 Python 中,解释器在概念上只是在运行过程中对其进行解释(有字节码编译,但这不是重点)。
尝试更改顺序,以便调用位于定义之后:
def get_func(self): # first define
...
get_func() # now invoke it
关于python - pycharm 中未解析的引用 get_func(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35358287/