# non-keyword variable-length arguments (tuple *args), converting n non-keyword parameter parameters to tuples.
# when a function is called, all formal parameters (both mandatory and default) are assigned to the corresponding local variables in the function declaration, and the remaining non-keyword parameters are inserted sequentially into a tuple. The
# variable-length parameter tuples must be after the positional and default parameters.
def func (arg1, arg2 = 9, *args):
print ("arg1:%d"% arg1)
print ("Arg2: %d "% arg2)
for I in args:
print (" Another arg:%d "% i)
#func (1, 2)
#func (1, 2, 3, 4, 5)
#func (1, 2, * (3, 4, 5)) # * (3, 4, 5) = tuple (3, 4, 5)
# keyword variable-length parameter (dictionary **kwargs), convert n keyword parameters into a dictionary.
def func1 (name, age=25, **kwargs):
Print (name)
Print (age)
For key in Kwargs:
Print (Kwargs[key])
Func1 (' Jack ', *, sex = ' male ', job = ' Engineer ')
def func2 (name, age=13, *args, **kwargs):
Print ("Name is%s"% name)
Print ("Age was%d"% age)
For info in args:
Print ("Class info:%s"% info)
For key in Kwargs:
Print ("Other information:%s"% kwargs[key])
Func2 (' xiaoming ', ' Class2 ', ' Grade1 ', job= ' Student ', score= ' 100 ')
# The positional parameter must precede the keyword argument.
Variable-length parameters for Python functions