Kwargs and Args in Python
Before I actually started working as a software engineer, kwargs and args have been one of those things in python that isn’t an absolute necessity to understand because there’s no requirement to actually use them in straightforward circumstances. However, they can be used as powerful tools, and therefore it was worth it to just take a moment and play around with the possibilities, and in this case I just made sure to document the walk-through to be able to post on the internets. But first, a worthy intro:
For starters, any list inside of python’s function parameters must follows the pattern of args and kwargs, or rather, a tuple and a dictionary, whether *args and **kwargs are even used at all.
For example, the typical list of function parameters may look something like this:
If we try and re-order the parameters such that those with default parameters come before the ones that do not, the python interpreter will throw an error saying “This isn’t Vietnam, we have rules here!” or something to that effect.
Before digging any deeper, let’s first ask ourselves a typical scenario in which we’d want to take advantage of args and kwargs. An obvious case would be one in which we’ve built a Django application, but we want to make sure we that we Rick Roll anyone named “Dale Hui” or anyone else we discover went to a rival high school instead of returning the actual request that they want.
This nice decorator can now be used on any function that has a “user” parameter!
Otherwise, kwargs can be used in a number of other basic fashions. We can substitute the arguments with a dictionary:
We can substitute **kwargs for function parameters:
We can combine the above two and just pass dictionaries all over the place:
This is a little move I like to call “The Smooth Sailor.” Whether or not anyone else calls it that, I have no idea. In the below example we might be able to make our code read a little moar straightforward and readable while re-using parameters from past function calls:
I like to call this move “The Crouching Soldier.”
Here we defined a function in advance that could return a number of different class types (in this case 2) with some kwargs already pre-defined. Now the code that follows is far less verbose and more readable.
We can also use the same type of behavior with args, which is just a tuple, and the parameters here can be ascertained by the values’ respective position in the tuple. Quick samples:
This is a move that I like to call “The Python Boomstick”. In the spirit of loose coupling and pure laziness, I can pass an arbitrary number of arguments to any function with *args or **kwargs as the parameters, as seen below:
Not even with args:
This is unfortunate, because as a programmer, I could have taken laziness to the next level. But I’ll survive. In conclusion, I’ll leave you with some inspirational code taken from the compiler seen in the movie Swordfish. Here you’ll see that the compiler is so advanced that it can generate IPv4 addresses using octets that create values greater than 255.