Search code examples
pythonpython-3.xbuilt-in

python how to return built in function


i'm having a difficult to understand the concepts of built-in functions, i have this piece of code:

def reversed_args(f):  # I can only change code in this function
    a = args[::-1]
    if func_name in int_func_map:
        resultados = int_func_map[func_name](*a)
    else:
        resultados = string_func_map[func_name](*a)
    return resultados


int_func_map = {
    'pow': pow,
    'cmp': lambda a, b: 0 if a == b else [1, -1][a < b],
}

string_func_map = {
    'join_with': lambda separator, *args: separator.join(args),
    'capitalize_first_and_join': lambda first, *args: ''.join([first.upper()] + list(args)),
}

queries = 1
for _ in range(queries):
    line = input().split()
    func_name, args = line[0], line[1:]
    if func_name in int_func_map:
        args = list(map(int, args))
        print(reversed_args(int_func_map[func_name])(*args))
    else:
        print(reversed_args(string_func_map[func_name])(*args))

my imput is

pow 2 3

but i'm receiving an exception saying that "'int' object is not callable" when i return the anwers, anyone can point me why


Solution

  • after some time wondering, I have managed to fulfill my needs, here's the piece of code that I made to make it run:

    def reversed_args(f):
        if func_name in int_func_map:
            def resultado(*kwargs):
                return int_func_map[func_name](*args[::-1])
        else:
            def resultado(*kwargs):
                return string_func_map[func_name](*args[::-1])
        return resultado
    

    I suppose that there's a better way to do it, but I did not find it