Search code examples
pythontypeerrorkeyword-argument

TypeError in python which says that dict object is not callable


I'm new to Python. I am getting the error TypeError:dict object is not callable. I haven't used dictionary anywhere in my code.

def new_map(*arg1, **func): 
    result = []
    for x in arg1:
        result.append(func(x))
    return result

I tried calling this function as follows:

new_map([-10], func=abs)

But when I run it, I am getting the above error.


Solution

  • Seems like you are using arbitrary arguments when they are not required. You can simply define your function with arguments arg1 and func:

    def new_map(arg1, func):
        result = []
        for x in arg1:
            result.append(func(x))
        return result
    
    res = new_map([-10], abs)
    
    print(res)
    
    [10]
    

    For detailed guidance on how to use * or ** operators with function arguments see the following posts: