- Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathtest_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.
"""
deftest_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:
deftest_function(first_param, *arguments):
"""This function accepts its arguments through "arguments" tuple"""
assertfirst_param=='first param'
assertarguments== ('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.
defconcat(*args, sep='/'):
returnsep.join(args)
assertconcat('earth', 'mars', 'venus') =='earth/mars/venus'
assertconcat('earth', 'mars', 'venus', sep='.') =='earth.mars.venus'