Python · Functions: arguments and return values
Defining functions, positional vs keyword arguments, default values, the mutable-default trap, and returning several values.
def name(params): defines a function, and a docstring right under the def line documents it. If a function has no return, it returns None.
Arguments can be passed by position or by keyword (f(x=1)), and parameters can have default values.
The classic trap: default values are evaluated once, when the def runs. A mutable default like [] is therefore shared by every call. The fix is to default to None and create the list inside the function.
return a, b returns a single tuple, and the caller unpacks it.
def grade(marks, total=100):
"""Return percentage and letter grade."""
pct = 100 * marks / total
letter = "A" if pct >= 85 else "B" if pct >= 70 else "C"
return pct, letter
p, g = grade(88)
print(p, g)
print(grade(total=50, marks=40))
def bad_append(x, items=[]):
items.append(x)
return items
def good_append(x, items=None):
if items is None:
items = []
items.append(x)
return items
print(bad_append(1), bad_append(2)) # shared list!
print(good_append(1), good_append(2))
print(grade.__doc__)
Output:
88.0 A
(80.0, 'B')
[1, 2] [1, 2]
[1] [2]
Return percentage and letter grade.Sources
- Defining Functions — Python Software Foundation
- More on Defining Functions — Python Software Foundation
- Programming FAQ: default values shared between objects — Python Software Foundation