The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow.
Finally, the least frequently used option is to specify that a function can be called with an arbitrary number of arguments. These arguments will be wrapped up in a tuple
python
def add_all(*numbers): print(type(numbers)) total = 0 for n in numbers: total += n return totalprint(add_all(1, 2, 3))
Output
<class 'tuple'>
6
Your output
Arbitrary Keyword Arguments (**kwargs)
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.
python
def print_info(**info): print(type(info)) for k, v in info.items(): print(f"{k}: {v}")print_info(name="Alice", age=30)
Output
<class 'dict'>
name: Alice
age: 30
Your output
Check yourself
Functions always have to return a value using the return keyword.
Reveal answer
If a return statement is omitted, Python automatically returns None. — If a return statement is omitted, Python automatically returns None.
You can mix positional and keyword arguments in any order when calling a function.
Reveal answer
In a function call, keyword arguments must always follow positional arguments. — In a function call, keyword arguments must always follow positional arguments.
Variables created inside a function are available globally.
Reveal answer
Variables created in a function live in a local symbol table and disappear when the function exits. — Variables created in a function live in a local symbol table and disappear when the function exits.
*args and **kwargs are reserved keywords.
Reveal answer
The single and double asterisks do the work. The names args and kwargs are just strong community conventions. — The single and double asterisks do the work. The names args and kwargs are just strong community conventions.
Challenges
Challenge 1 +15 XP
Write a function make_profile that takes a name and an optional status that defaults to active. It should return a dictionary with those keys.
Write a function build_url that takes a base string and arbitrary keyword arguments (**kwargs). It should print the base url followed by a question mark and the kwargs dictionary.
python
Test 1 — expects "http://test.com?{'q': 'python', 'page': 2}\n"
Need a hint? (−25% XP)
The **kwargs parameter captures all extra keyword arguments as a dictionary.