Saltycrane logo

Sofeng's Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

How to use *args and **kwargs in Python

Or, How to use variable length argument lists in Python.

The special syntax, *args and **kwargs in function definitions is used to pass a variable number of arguments to a function. The single asterisk form (*args) is used to pass a non-keyworded, variable-length argument list, and the double asterisk form is used to pass a keyworded, variable-length argument list. Here is an example of how to use the non-keyworded form. This example passes one formal (positional) argument, and two more variable length arguments.

def test_var_args(farg, *args):
    print "formal arg:", farg
    for arg in args:
        print "another arg:", arg

test_var_args(1, "two", 3)

Results:

formal arg: 1
another arg: two
another arg: 3

Here is an example of how to use the keyworded form. Again, one formal argument and two keyworded variable arguments are passed.

def test_var_kwargs(farg, **kwargs):
    print "formal arg:", farg
    for key in kwargs:
        print "another keyword arg: %s: %s" % (key, kwargs[key])

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

Results:

formal arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3

Using *args and **kwargs when calling a function

This special syntax can be used, not only in function definitions, but also when calling a function.

def test_var_args_call(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3

args = (1, "two", 3)
test_var_args_call(*args)

Results:

arg1: 1
arg2: two
arg3: 3

Here is an example using the keyworded form when calling a function:

def test_var_args_call(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3

kwargs = {"arg3": 3, "arg2": "two", "arg1": 1}
test_var_args_call(**kwargs)

Results:

arg1: 1
arg2: two
arg3: 3

Of course, this syntax can be used in both the function call and the function definition, but I don't think an example for that is necessary.

See also Section 5.3.4 in the Python Reference Manual

Reference: Core Python Programming, Second Edition, Section 11.6


2 Legacy Comments


#1 Skawaii commented, on February 13, 2008 at 1:46 p.m.:

Thanks for the succinct explanation of *args and **kwargs. You made it nice and easy to understand.


#2 Julien commented, on May 16, 2008 at 8:01 a.m.:

thanks for this explanation. It's pretty cool to have such informations easy to understand.

6 New Comments


#1 Dmitry commented, on October 28, 2008 at 12:09 a.m.:

Thanks for your explanation.


#2 mike commented, on October 29, 2008 at 1:39 p.m.:

Thanks just learnt python from Think Python free book, but there was no mention of this. I was seeing it frequently in other peoples code and it was confusing me. Not now. Thanks again!


#3 sofeng commented, on October 29, 2008 at 6:34 p.m.:

Mike, I had not heard of Think Python. It looks like a great free Python resource.


#4 Katja commented, on October 31, 2008 at 10:43 a.m.:

Awesome! This is THE explanation I was looking for :).


#5 Volkan commented, on November 13, 2008 at 2:56 a.m.:

I have newly discovered your blog and it is awsome. Also this is a very good tip. Thanks you!


#6 Richie commented, on November 22, 2008 at 2:35 p.m.:

I too want to thank you for your explanation. Just started Python/Django and blogs like this really speed up the task.

Post a comment

: Required
Email: Required, but not displayed
Website: Optional
:

Format using Markdown. (HTML not allowed.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
:

Created with Django | Hosted by Webfaction