Open GoogleCodeExporter opened 9 years ago
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
Original comment by levchenk...@gmail.com
on 12 Jul 2011 at 11:58
Thanks for the recommendation. This is the first time I've received a
recommendation for this feature... and I think it is a good idea. I will
investigate the implementation. The code you provided can serve as a couple
unit tests. I've re-filed this as Type-Enhancement rather than defect.
Original comment by dwhall...@gmail.com
on 15 Jul 2011 at 2:29
Original issue reported on code.google.com by
levchenk...@gmail.com
on 12 Jul 2011 at 11:55