Why this unpacking of arguments does not work? Why this unpacking of arguments does not work? python python

Why this unpacking of arguments does not work?


The ** syntax requires a mapping (such as a dictionary); each key-value pair in the mapping becomes a keyword argument.

Your generate() function, on the other hand, returns a tuple, not a dictionary. You can pass in a tuple as separate arguments with similar syntax, using just one asterisk:

create_character = player.Create(*generate_player.generate())

Alternatively, fix your generate() function to return a dictionary:

def generate():    print "Name:"    name = prompt.get_name()    print "Age:"    age = prompt.get_age()    print "Gender M/F:"    gender = prompt.get_gender()    return {'name': name, 'age': age, 'gender': gender}


You just want a single asterisk:

create_character = player.Create(*generate_player.generate())

You're passing a sequence of arguments, for which you use one asterisk. The double-asterisk syntax is for passing a mapping, for instance to do something like this:

player.Create(**{'name': 'Richie', 'age': 21, 'gender': 'male'})


FWIW, in my case I left a comma , at the end of the definition of the dictionary, e.g.,

params = {...}, f(**params)

and params is a 1-tuple now with the dictionary being its element, hence the error... Removing the comma resolves it.