* args and ** kwargs in Python

Python Methods and Functions

The special syntax * args in function definitions in python is used to pass a variable the number of arguments to a function. It is used to pass a variable-length argument list without a key.

  • The syntax is to use the * character to accept a variable number of arguments; by convention, this is often used with the word args.
  • * Args allows you to take more arguments than the number of formal arguments you defined earlier. With * args , any number of additional arguments can be bound to your current formal parameters (including zero additional arguments).
  • For example: we want to create a multiplication function that takes any number of arguments and able to multiply them all together. This can be done with * args.
  • By using *, the variable we bind to * becomes iterative, which means you can do things like iterate over it, run some higher order functions such as map and filter, etc.
  • * arg usage example:

    # Python program for illustration
    # * arguments for variable number of arguments

    def myFun ( * argv): 

    for arg in argv: 

    print ( arg)

     

    myFun ( 'Hello' , ' Welcome' , 'to' , ' GeeksforGeeks'

    Exit:

     Hello Welcome to Python.Engineering 

    # Python program for illustration
    # * arguments with the first optional argument

    def myFun (arg1, * argv):

    print ( "First arg ument: " , arg1)

      for arg in argv:

    print ( " Next argument through * argv: " , arg)

      

    myFun ( 'Hello' , ' Welcome' , 'to' , 'GeeksforGeeks' )

    Exit:

     First argument: Hello Next argument through * argv: Welcome Next argument through * argv: to Next argument through * argv: Python.Engineering 
** kwargs

The special syntax ** kwargs in function definitions in python is used to pass a variable length keyed list of arguments. We use the name kwargs with a double star. The reason is that the double star allows us to pass through keyword arguments (and any number of them).

  • The keyword argument — this is where you provide the name of the variable when you pass it to the function.
  • You can think of kwargs as a dictionary that maps each keyword to the value we pass together with him. This is why, when we iterate over kwargs , there seems to be no order in which they would be printed.
  • Example of using ** kwargs:

    # Python program for illustration
    # * kargs for variable number of keyword arguments

     

    def myFun ( * * kwargs): 

    for key, value in kwargs.items ():

    print ( "% s ==% s" % (key, value))

     
    # Driver code

    myFun (first = 'Geeks' , mid = 'for' , last = ' Geeks'

    Output:

     last == Geeks mid == for first == Geeks 

    # Python program to illustrate ** karg for
    # variable number of keyword arguments with
    # one additional argument.

     

     

    def myFun (arg1, * * kwargs): 

      for key, value in kwargs.items ():

    print ( "% s ==% s" % (key, value))

     
    # Driver code

    myFun ( " Hi " , first = 'Geeks' , mid = 'for' , last = ' Geeks'

    Exit:

     last == Geeks mid == for first == Geeks 

Using * args and ** kwargs to call a function

Examples:

def myFun (arg1, arg2, arg3):

  print ( "arg1:" , arg1)

print ( "arg2:" , arg2)

print ( "arg3:" , arg3)

 
# Now we can use * args or ** kwargs for
# pass arguments to this function:

args = ( "Geeks" , " for " , " Geeks " )

myFun ( * args)

 

kwargs = { "arg1" : "Geeks" , "arg2" : "for" < code class = "plain">, "arg3" : "Geeks " }

myFun ( * * kwargs)

Output:

 arg1: Geeks arg2: for arg3: Geeks arg1: Geeks arg2: for arg3: Geeks 

This article is provided by Kishlai Verma . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.