What the hell are “**kwargs”…

…aside from the sound you made when you vomitted that night you drank too much tequila (oh wait, programmers only drink local IPAs from underground breweries run by your sweaty cousin).

Now…I’m annoyed because I had to look up like 20 different things just to get to this one explanation of what a kwarg is but you know I do anything for you, baby. And let me just say…if you’re trying to bring me your sassy responses about kwargs like this exchange below, I’m not here for it.

How condescending of you, S.Lott

Let me explain this to you as if I were explaining it to my grandma, who knows nothing about programming (not that I’m saying my grandma is dull — she’s a very smart woman).

Janice, “kwargs” literally stands for key-word arguments.

The argument part of **kwargs is what you want to pass through a function, or executable statement, in your code. In computer programming, writing a function is like assigning little (or big) tasks to one word or step. A function makes it so that instead of writing out the steps over and over and over again in a waterfall-type shape, you can just define everything you want a script to do in a little wrapper or vessel word, and then you can pass arguments through that container. It’s cowboy classy (but it’s not a class). Yee-haw.

The key-word part of **kwargs means that the argument you want to pass through the function is in the form of a dictionary, so, just like an actual dictionary, it has key-words assigned to values or definitions, as we’ll see in the example below.

And lastly, the ** part of **kwargs is a syntax thingy in Python that just forces the function to treat your argument as a dictionary format. I would say… don’t worry about it and worry about it at the same time. As in…leave the ** in the damn script alone and let them do their thing.

Below, we are trying to make a cute little Python program whose output will use keyword arguments (**kwargs). Do you see how we are creating a “print_values” function and we are asking it to treat all future arguments as key-word pairs? This script creates a dictionary argument to pass through the function on a very simple level, but you could also pass many **kwargs through a function on a large-scale level.

So next time you see **kwargs in a piece of code, have no fear. You know what this is. Your program is trying to pass both a key word and its value through one function without going through the hassle of writing that all into the script. You’re just trying to stick two things into one… that’s what she said.

And thank you kindly to this nice page by DigitalOcean that explains **kwargs very nicely and not in Greek:

https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3