Python *args and **kwargs
When i started out with python, the *args and **kwargs concept was my initial hurdle which took me a while to get past.And ever since i understood how it functions, i have found it very useful and thought it would be helpful writing a post on this for beginners.
*args and **kwargs stands for arguments and key word arguments . The name are just conventions and can be replaced with anything of your choice. *args and **kwargs are mainly used to pass values to function in convenient pythonic way.
So lets say we have a function test.
######################################################
def test(vari , *args, **kwargs):
print “variable :”,vari
if args: print “args :”, args[0], args[1], args[2]
for arg in args: print “arg :”, arg
if kwargs['check']: print “check = ”kwargs['check']
for key, value in kwargs.items(): print “key: “,key print “value :“,value
test(3,4,”string”, 6, check="some_string", rest="another_string")
########################################################
In the above example, the output would be
output:
########################################################
variable : 3 args : 4 strin 6 arg : 4 arg : strin arg : 6 check = some_string key: check value : some_string key: rest value : another_string
########################################################
In the above example, when we look at the order in which we pass the values to a function, it should always be,
-formal or a regular varible, followed by *args and then by **kwargs.
In the example above when we passed multiple arguments to the test fucntion, the first value is assigned to the vari(fromal varible) varible , which is 3.
The list of arguments that follows are(we can pass any no of arguments) what is split between *args and **kwargs
*args
args are like lists. Any value which you pass to a function,(leaving the formal varibles value, as it gets assigned right at the beginning) without the “key and value pair” like say (eg above), check = “some_string” falls under args. In the above case , values such as (4 , “string” and 6) gets assigned to the *args.
**kwargs
kwargs are like dictionaries. therfore they need both a key and a value to be assigned. which is why all values passed with both key and value , like say (check = “some_string” and rest = “another_string”) falls under kwargs.
if you undestand this concept, accessing values from *args and **kwargs becomes as simple as accessing values from lists and dictionaries. As shown above i can either access individual values from args using args[0], args[1] and so on, or iterate through all the value, like how we do with a list.
similarly, you can access individual values from **kwargs just like how you do it with a dictionary using the key -- kwargs[’check’] which will give the value of it “some_string” or we can iterate through it, like
for key, value in kwargs.items(): print “key: “,key print “value :“,value
just like a dictionary.
An another important concept is passing the args and kwargs to a function.
suppose i have a list which i need to pass to *args varible or a dictionary which i need to pass to kwargs variable of a function. I could do that by
li = [4,”string”,6]
dict_values = {”check” : “some_string”, “rest” : “another_string”}
test(3, *li, **dict_values)
and this would give out the same result.
I hope this post was easy to follow and helpful in understanding things. Let me know if you face any difficulties and i would be more than happy to help you out.














