Understanding arguments *args and **kwargs in Python


In Python, we usually need passing variable through arguments to functions. There are 2 things that commonly need to know about passing arguments into function which called *args and **kwargs. What this is ?

*args
This single asterisk form is used to pass non-keyworded, variable-length argument list. For instance :

1
2
3
4
5
6
7
# Single asterisk form
def someFunction(farg, *args):
    print "Basic arg : ", farg
    for arg in args:
        print "single asterisk form arg : ", arg

someFunction(1, "testing", 30)

Will give result :

1
2
3
Basic arg :  1
single asterisk form arg :  testing
single asterisk form arg :  30


Now we try implementing *args into function :

1
2
3
4
5
6
7
8
9
# Single asterisk form
def someFunction(farg, *args):
    print "Basic arg : ", farg
    for arg in args:
        print "single asterisk form arg : ", arg

# create tuple args
args = (1, 3)
someFunction(1, *args)

Give results :

1
2
3
Basic arg :  1
single asterisk form arg :  1
single asterisk form arg :  3

**kwargs
This is double asterisk form is used to pass keyworded, key variable argument list. How to implement this arguments into function ? Here are :

1
2
3
4
5
6
def someFunction2(farg, **kwargs):
    print "Basic arg : ", farg
    for key in kwargs:
        print "another keyword arg: %s %s" % (key, kwargs[key])

someFunction2(farg=1, myarg2="two", myarg3=3)

Will give result :

1
2
3
Basic arg :  1
another keyword arg: myarg2 two
another keyword arg: myarg3 3

You will see that **kwargs is like dictionaries which have key and value. Differences with **kwargs are :
1. You can specify parameters as you want (a=2,b=”two”, …. ).
2. You can calculate parameter length inside kwargs by kwargs length.

**kwargs help us to make people enter argument into function with parameters and value. For example, we want someFunction() is filled with arguments a=1, b=”two”. People not allowed to enter argument (1, “two”). Also, we not allowed another parameters except “a” and “b”. Here are the code :

1
2
3
4
5
6
7
8
def someFunction3(**kwargs):
    expected_args = ["a", "b"]

    for key in kwargs:
        if key in expected_args:
            print kwargs[key]

someFunction3(a=1, b="two", c=’z’)

Give result :

1
2
1
two

Another implementation

1
2
3
4
5
6
7
8
def someFunction2(farg, **kwargs):
    print "Basic arg : ", farg
    for key in kwargs:
        print "another keyword arg: %s %s" % (key, kwargs[key])

# kwargs using dictionaries
kwargs = {"a": 1, "b": "two"}
someFunction2(farg=1, **kwargs)

Give result :

1
2
3
Basic arg :  1
another keyword arg: a 1
another keyword arg: b two

Another things to remember, *args is like tuple and **kwargs is like dictionary. Now you should check all your function and implement this *args and **kwargs to improve your python basic knowledge 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.