Search code examples
pythonclass-method

Python, Error: "function is not defined" ,


I have 2 @classmethod: one of them is 'operators' and other one is 'pretty_print_2'. in 'operators' i need to call 'pretty_print_2'.

The first method is as follow:

    @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)

The second method is as follow:

@classmethod
def pretty_print_2(response):

but when i run the script I faced the following error:

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

I will be appreciated for any suggestion. Thank you.


Solution

  • First argument in class methods should be cls i.e. the class itself. So your method:

    @classmethod
    def pretty_print_2(response)
    

    should look like:

    @classmethod
    def pretty_print_2(cls, response)