TypeError in python which says that dict object is not callable


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.


TypeError:dict object is not callable


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)


new_map([-10], func=abs)



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




5 Answers
5



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


arg1


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:


*


**





Thanks for you help but arg1 is iterable here. So I had put *arg1. How to differentiate key word argument from positional argument if I don't put a * before func?
– Shreesakhi
Jun 29 at 11:48





Try it, test it, you don't need *! You have 2 arguments, one of them is an iterable, the other is a function. Your function doesn't need to unpack arbitrary arguments, because they are well defined.
– jpp
Jun 29 at 11:53



*





when tried to test your code with the following test case: new_map(-10, -20, -30, func=abs). It shows the following error: new_map() takes 1 positional argument but 3 positional arguments (and 1 keyword-only argument) were given
– Shreesakhi
Jun 29 at 11:58





Use input [-10, -20, -30] instead, i.e. new_map([-10, -20, -30], abs). Don't choose lists or unpacked elements arbitrarily, stick with one or the other.
– jpp
Jun 29 at 11:58



[-10, -20, -30]


new_map([-10, -20, -30], abs)





Since the arbitrary argument may vary for each test case, I have used it that way
– Shreesakhi
Jun 29 at 12:01



The ** prefix says that all of the keyword arguments to your function should be grouped into a dict called func. So func is a dict and func(x) is an attempt to call the dict and fails with the error given.


**


dict


func


func


dict


func(x)


dict



You have used a dictionary by mistake. When you defined new_map(*arg1, **func), the func variable gathers the named parameter given during the function call. If func is supposed to be a function, put it as first argument, without * or **


new_map(*arg1, **func)


func


func


*


**



func is a dictionary in your program. If you want to access value of it then you should use not (). Like:


func


dictionary



()


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



If func is a function to your program then you should write:


func


function


def new_map(*arg1, func):
result =
for x in arg1:
result.append(func(x)) #use , not ()
return result



Or a simple list comprehension:


def new_map(arg1, func):
return [func(i) for i in arg1]

out = new_map([-10], func=abs)
print(out)



Output:


[10]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift