Python, Error: "function is not defined" ,

Python, Error: "function is not defined" ,

我有 2 个 @classmethod:其中一个是 'operators',另一个是 'pretty_print_2'。 在 'operators' 我需要打电话给 'pretty_print_2'。

第一种方法如下:

    @classmethod
def operators(cls, operator_filter=None, limit_filter=0, ncores=1, exec_mode='sync'):


    ...

    response = None
    try:
        if Cube.client is None:
            raise RuntimeError('Cube.client is None')

        query = 'oph_operators_list '

        if operator_filter is not None:
            query += 'operator_filter=' + str(operator_filter) + ';'
        if limit_filter is not None:
            query += 'limit_filter=' + str(limit_filter) + ';'
        if ncores is not None:
            query += 'ncores=' + str(ncores) + ';'
        if exec_mode is not None:
            query += 'exec_mode=' + str(exec_mode) + ';'

        if Cube.client.submit(query) is None:
            raise RuntimeError()

        if  Cube.client.last_response is not None:
            response = Cube.client.deserialize_response()


    except Exception as e:
        print(get_linenumber(), "Something went wrong:", e)
        raise RuntimeError()
    else:

        cls.pretty_print_2(response)

第二种方法如下:

@classmethod
def pretty_print_2(response):

但是当我 运行 脚本时,我遇到了以下错误:

 cls.pretty_print_2(response)
TypeError: pretty_print_2() takes 1 positional argument but 2 were given

如有任何建议,我将不胜感激。 谢谢。

class 方法中的第一个参数应该是 cls,即 class 本身。所以你的方法:

@classmethod
def pretty_print_2(response)

应该看起来像:

@classmethod
def pretty_print_2(cls, response)