92

I'm new at Python, and I'm trying to basically make a hash table that checks if a key points to a value in the table, and if not, initializes it to an empty array. The offending part of my code is the line:

converted_comments[submission.id] = converted_comments.get(submission.id, default=0)

I get the error:

TypeError: get() takes no keyword arguments

But in the documentation (and various pieces of example code), I can see that it does take a default argument:

https://docs.python.org/2/library/stdtypes.html#dict.get http://www.tutorialspoint.com/python/dictionary_get.htm

Following is the syntax for get() method:

dict.get(key, default=None)

There's nothing about this on The Stack, so I assume it's a beginner mistake?

Community
  • 1
  • 1
itsmichaelwang
  • 2,282
  • 4
  • 16
  • 25
  • 3
    The example in the second link is funny. – Ziyuan Feb 26 '15 at 12:25
  • a bit offtopic, but you can use `defaultdict` from the `collections` module to achieve the same functionality. – moshevi Jan 24 '19 at 09:01
  • people might hate me for suggesting this but I've being doing this for range since I have a terrible memory of what the order of things are. Imo this shouldn't be a problem so I'm fixing it: `range(*{'start':0,'stop':10,'step':2}.values())` – Charlie Parker Jun 12 '20 at 18:53

3 Answers3

196

Due to the way the Python C-level APIs developed, a lot of built-in functions and methods don't actually have names for their arguments. Even if the documentation calls the argument default, the function doesn't recognize the name default as referring to the optional second argument. You have to provide the argument positionally:

>>> d = {1: 2}
>>> d.get(0, default=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: get() takes no keyword arguments
>>> d.get(0, 0)
0
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 46
    You hinted, but didn't say explicitly: python functions usually allow arbitrary named arguments. The C-level API is an exception to the rule. – idbrii Aug 07 '15 at 17:22
  • Thanks, although it's more annoying if I want to debug current code – Dejell Feb 14 '17 at 08:43
  • 4
    Wow I used python for 7 years, and I never knew there were functions that don't take keyword arguments. That's so strange, surely the interpreter could maintain a dictionary of builtin functions and their argument names, so that it could accept keyword arguments with nearly zero runtime cost? (The only cost would be during the generation of bytecode, and it would be very small.) – max Jun 19 '17 at 01:19
  • 3
    @max: Parameter names can't be resolved at compile time, because what callable you're calling can't be resolved at compile time. Making it a compile-time thing wouldn't intrinsically make all C functions and methods take keyword arguments, anyway; either way, people would have to go through and add all the missing parameter name metadata by hand, and they could (but don't) do that under the current design. – user2357112 Jun 19 '17 at 01:38
40

The error message says that get takes no keyword arguments but you are providing one with default=0

converted_comments[submission.id] = converted_comments.get(submission.id, 0)
GWW
  • 43,129
  • 11
  • 115
  • 108
12

Many docs and tutorials, for instance https://www.tutorialspoint.com/python/dictionary_get.htm, erroneously specify the syntax as

dict.get(key, default = None)

instead of

dict.get(key, default)

Bertel
  • 149
  • 2
  • 6