1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shsa
  • 17
  • 10

5 Answers5

1

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.

Duncan
  • 92,073
  • 11
  • 122
  • 156
0

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:

jpp
  • 159,742
  • 34
  • 281
  • 339
  • 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? – Shsa Jun 29 '18 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 '18 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 – Shsa Jun 29 '18 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 '18 at 11:58
  • Since the arbitrary argument may vary for each test case, I have used it that way – Shsa Jun 29 '18 at 12:01
  • Your function will be overly and unnecessarily complex if you use `[-10]` in one case or `-10` in another. This is just an opinion to make your life easier :) – jpp Jun 29 '18 at 12:02
0

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 **

blue_note
  • 27,712
  • 9
  • 72
  • 90
0

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

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:

def new_map(*arg1, func): 
    result = []
    for x in arg1:
        result.append(func(x)) #use [], not ()
    return result
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39
0

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]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114