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
*args and **kwargs when calling a functionThis 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
Thanks for the succinct explanation of *args and **kwargs. You made it nice and easy to understand.
thanks for this explanation. It's pretty cool to have such informations easy to understand.
Thanks for your explanation.
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!
Mike, I had not heard of Think Python. It looks like a great free Python resource.
Awesome! This is THE explanation I was looking for :).
I have newly discovered your blog and it is awsome. Also this is a very good tip. Thanks you!
I too want to thank you for your explanation. Just started Python/Django and blogs like this really speed up the task.
aws
(3)
bison_flex
(1)
blogger
(4)
c
(10)
colinux
(2)
concurrency
(8)
conkeror
(2)
cygwin
(17)
dell
(3)
django
(25)
eclipse
(30)
emacs
(18)
email
(1)
error
(10)
gnip
(1)
json
(1)
keyboard
(3)
linux
(27)
matplotlib
(5)
mercurial
(3)
openid
(1)
personal
(3)
preferences
(3)
pyqt
(18)
python
(88)
rails
(1)
ratpoison
(3)
recursion
(1)
rsync
(3)
ruby
(2)
sql
(8)
subversion
(4)
twisted
(5)
ubuntu
(29)
untagged
(7)
urxvt
(3)
vxworks
(26)
wmii
(3)