-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
test_function_arbitrary_arguments.py
33 lines (25 loc) · 1.65 KB
/
test_function_arbitrary_arguments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Arbitrary Argument Lists
@see: https://docs.python.org/3/tutorial/controlflow.html#arbitrary-argument-lists
Function can be called with an arbitrary number of arguments. These arguments will be wrapped up in
a tuple. Before the variable number of arguments, zero or more normal arguments may occur.
"""
def test_function_arbitrary_arguments():
"""Arbitrary Argument Lists"""
# When a final formal parameter of the form **name is present, it receives a dictionary
# containing all keyword arguments except for those corresponding to a formal parameter.
# This may be combined with a formal parameter of the form *name which receives a tuple
# containing the positional arguments beyond the formal parameter list.
# (*name must occur before **name.) For example, if we define a function like this:
def test_function(first_param, *arguments):
"""This function accepts its arguments through "arguments" tuple"""
assert first_param == 'first param'
assert arguments == ('second param', 'third param')
test_function('first param', 'second param', 'third param')
# Normally, these variadic arguments will be last in the list of formal parameters, because
# they scoop up all remaining input arguments that are passed to the function. Any formal
# parameters which occur after the *args parameter are ‘keyword-only’ arguments, meaning that
# they can only be used as keywords rather than positional arguments.
def concat(*args, sep='/'):
return sep.join(args)
assert concat('earth', 'mars', 'venus') == 'earth/mars/venus'
assert concat('earth', 'mars', 'venus', sep='.') == 'earth.mars.venus'