Home » Web

How to use *args and **kwargs in Python

21 March 2011 No Comment
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

How to use *args and **kwargs in PythonAre there times when you want to specify a variable argument list in Python?

This is possible by using *args and **kwargs.

The special syntax, *args and **kwargs, in a function definition is used to pass a variable number of arguments to a function.

The single asterisk (*args) is used to pass a non-keyword, variable length argument list. The double asterisk (**kwargs) is used to pass a keyword, variable length argument list.

In this example, a function passes one required argument and three variable arguments.

def pass_var_args(first_arg, *args):
  print 'required argument: ', first_arg
  for arg in args:
    print 'variable argument: ', arg

pass_var_args(5, 'ten', 15, 'twenty')

The result shows how each argument is looped through and passed as arg.

required argument: 5
variable argument: ten
variable argument: 15
variable argument: twenty

This is an example of using one required argument and three keyword arguments.

def pass_key_args(first_arg, **kwargs):
  print 'required argument: ', first_arg
  for key in kwargs:
    print 'variable argument: %s %s' % (key, kwargs[key])
pass_key_args(arg1=5, arg2='ten', arg3=15, arg4='twenty')

The result shows how the key and value appear in the output.

required argument: arg1 5
variable argument: arg2 ten
variable argument: arg3 15
variable argument: arg4 twenty

This is an example of calling a function and passing a list.

def pass_multi_args(arg1, arg2, arg3, arg4):
  print 'first argument: ', arg1
  print 'second argument: ', arg2
  print 'third argument: ', arg3
  print 'fourth argument: ', arg4

args = ['ten', 15, 'twenty']
pass_multi_args(5, *args)

The results shows the args list getting passed to the function.

first_argument: 5
second_argument: 'ten'
third_argument: 15
fourth_argument: 'twenty'

This is an example of calling a function and passing a keyword dictionary.

def pass_multi_key_args(arg1, arg2, arg3, arg4):
  print 'first argument: ', arg1
  print 'second argument: ', arg2
  print 'third argument: ', arg3
  print 'fourth argument: ', arg4
  kwargs = {'arg2': 'ten', 'arg3': 15, 'arg4': 'twenty'}
pass_multi_key_args(5, **kwargs)

The results shows the kwargs dictionary getting passed to the function.

first_argument: 5
second_argument: 'ten'
third_argument: 15
fourth_argument: 'twenty'

Incoming search terms:

  • python kwargs
  • python **kwargs
  • python kwargs example
(Visited 284 times, 1 visits today)

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.