Python-Ref > Basic usage > Functions > Creating functions with variable number of arguments
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Creating functions with variable number of arguments

Less constrained approach to creation of functions with optional arguments.
Sometimes it is inconvenient or even impossible to specify all the arguments of a function in advance and we need a mechanism to say that there might be any number of arguments.
This is possible both for normal and keyword arguments.
In the example below, function that takes any number of normal arguments is shown. All the 'unspecified' arguments are passed to the function as a tuple. To specify that a function takes optional arguments, one gives a name for a tuple that will take all the optional arguments preceded by a star (*).
Expand/Shrink
Zdroj: (func5-1.py)
  1   # two mandatory and infinit number of optional arguments
  2   def multiply( a, b, *cs):
  3       """this function returns the result of
  4       multiplication of all its arguments"""
  5       print "a =", a     # just show the arguments
  6       print "b =", b
  7       print "cs =", cs   # tuple of all optional arguments
  8       ret = a * b
  9       for c in cs:
 10           ret = ret * c
 11       return ret
 12   
 13   print multiply( 1, 2, 3, 4)
 14       
stdout:
a = 1
b = 2
cs = (3, 4)
24
Doba běhu: 29.0 ms
The name of a tuple for optional arguments must be given after all the mandatory and keyword arguments.
Similar approach is available for keyword arguments. In this case the arguments are available to the function as a dictionary of the structure keyword->value. The name of the dictionary appears in the function specification preceded by two stars (**).
Expand/Shrink
Zdroj: (func5-2.py)
  1   def show_arguments( a, keyword="default", **kws):
  2       """this function only prints all its arguments.
  3       It takes one mandatory argument, one specified keyword argument
  4       and infinite number of other keyword arguments."""
  5       print "a =", a
  6       print "keyword =", keyword
  7       print "kws =", kws
  8   
  9   
 10   show_arguments( 100)
 11   print   # just add empty line
 12   show_arguments( 20, keyword="non-default")
 13   print
 14   show_arguments( 17, other_keyword=1, yet_another="hobbit")
 15   print
 16   show_arguments( 17, other_keyword=1, yet_another="hobbit", keyword="anything")
stdout:
a = 100
keyword = default
kws = {}

a = 20
keyword = non-default
kws = {}

a = 17
keyword = default
kws = {'yet_another': 'hobbit', 'other_keyword': 1}

a = 17
keyword = anything
kws = {'yet_another': 'hobbit', 'other_keyword': 1}
Doba běhu: 21.2 ms
The name of a dictionary for optional keyword arguments must be given at the end of the function argument list.
It is of course possible to combine the two approaches described above and create functions that take any number of optional and keyword arguments.
Expand/Shrink
Zdroj: (func5-4.py)
  1   def show_arguments( mandatory, keyword="default", *optional, **optional_keywords):
  2       """this function only prints all its arguments.
  3       It takes one mandatory argument, one specified keyword argument
  4       and infinite number of optional and other keyword arguments."""
  5       print "mandatory =", mandatory
  6       print "keyword   =", keyword
  7       print "optional  =", optional
  8       print "opt. key. =", optional_keywords
  9   
 10   
 11   show_arguments( 1, 2, 3, 4)
stdout:
mandatory = 1
keyword   = 2
optional  = (3, 4)
opt. key. = {}
Doba běhu: 21.0 ms
It is not a good idea, however, to combine pre-specified keyword arguments with optional arguments. It may lead to some unexpected results.
From the examples above it is obvious that this approach is not so convenient as the normal keyword arguments approach because you need to handle the tuples and dictionaries of arguments afterwards. Therefor it is used only in cases when really necessary.