python - pycharm 中未解析的引用 get_func()

标签 python function

很抱歉,我刚刚开始使用 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/

相关文章:

Python-can OSError : An address incompatible with the requested protocol was used

python - 使用 Python Urwid/NCurses 在终端应用程序中创建水平和垂直可滚动表

c - 声明和定义存储在哪里?

PHP类函数命名

php - Wordpress 登录注销自定义功能错误

javascript - 如何使用 javascript 中的范围在最小值和最大值之间创建比例?

python - 提取子矩阵时出现问题

python - 在python中生成两个项目的所有可能长度n的组合

python - 使用 Python 3 捕获 192 kHz 音频

列表列表中的 Python 函数参数