Default, Keyword and Variable Arguments
About 18 minutes
Why this matters
Real functions are flexible. print("a", "b", sep="-") takes any number of values and an optional separator. You can write functions like that too.
The idea
Default values make a parameter optional:
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
greet("Ada") # 'Hello, Ada!'
greet("Ada", "Welcome") # 'Welcome, Ada!'
Keyword arguments say which parameter you mean, so the order no longer matters and calls read well:
greet(greeting="Hi", name="Bo")
*args collects any number of extra positional arguments into a tuple:
def total(*numbers):
return sum(numbers)
total(1, 2, 3) # 6
**kwargs collects extra keyword arguments into a dictionary:
def show(**info):
return info
show(a=1, b=2) # {'a': 1, 'b': 2}
A parameter placed after *args can only be given by keyword (sep= below). Parameters with defaults come after those without.
Try it
Write box(text, char="*") that returns the text surrounded by char on each side. Call it with and without the second argument.
Your turn
Write three functions that return their answers:
power(base, exponent=2):power(3)is9andpower(2, 10)is1024.join_words(*words, sep=" "):join_words("a", "b", "c")is"a b c", and withsep="-"it is"a-b-c".profile(name, **details): the name, then the details sorted by key:profile("Ada", city="London", age=36)is"Ada (age=36, city=London)". With no details it is just"Ada".
Recap
def f(x, y=10)givesya default; callers may leave it out.- Keyword arguments (
f(y=1, x=2)) name the parameter;*argsand**kwargsgather extras into a tuple and a dict. - Defaults are evaluated once: use immutable defaults like numbers, text or
None.
Common mistakes
- A required parameter after a default one:
SyntaxError: non-default argument follows default argument. - A list as a default (
def f(items=[])): the same list is shared by every call. UseNoneand create the list inside. - Giving a parameter twice, positionally and by keyword:
TypeError: got multiple values.