Python,错误:"函数未定义" ,

时间:2016-10-25 09:04:42

标签: python class-method

我有2 @classmethod:其中一个是'运营商'而另一个是“pretty_print_2'”。 在'运营商'我需要打电话给#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

对于任何建议,我将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:0)

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

@classmethod
def pretty_print_2(response)

应该是这样的:

@classmethod
def pretty_print_2(cls, response)