[PYTHON] If you don't know how many arguments you will have 2 ** kwargs Variadic arguments

If you don't know how many arguments * args variadic arguments are It was a tuple, Convert keyword arguments into a dictionary and perform the same processing.

dictionary


def players(**kwargs):
    for k, v in kwargs.items():
        print(k + 'Is' + v + 'is.')

d = {
    'Taro':'Brave',
    'Jiro':'Warrior',
    'Saburo':'Wizard',
    'Shiro':'Monk'
}

players(**d)

As before, dictionary d Expand with ** d, Pass it to the players function. ** Dictionary with kwargs. And That dictionary kwargs for k, v in kwargs.items(): With print (k +'is'+ v +'.') Use.

Dictionary execution result


Taro is a brave man.
Jiro is a warrior.
Saburo is a witch.
Shiro is a monk.

Recommended Posts

If you don't know how many arguments you will have 2 ** kwargs Variadic arguments
How to find if you don't know the Java installation directory
If you don't know it, it's dangerous. Carefully explain how to use the xargs command
Don't you know it? pip command
How many types of Python do you have in Windows 10? I had 5 types.
If you don't know how to draw the graph you want with matplotlib, it is convenient to look at the gallery.