← Back to course
Code Crew G6+ / Week 06 / Thursday
4/6
Week 06 Β· Functions and modules

Thursday

Defaults and docstrings
// Name a job once, use it everywhere
⏱ about 30 min

Thursday: Defaults and docstrings

The fuel function needs three numbers: the distance, the burn rate and a safety reserve. Almost every trip uses a rate of 3 and a reserve of 10, and Rocket is tired of typing them.

"Ninety percent of the time it is the same numbers," he says. "Can the function just remember them?"

Raven flips through her notes from a month ago. "What do you notice?" she asks. "We also forgot what this function was for. There is no note telling us."

Nova displays the function with two glowing default values and a short sentence underneath. "Would you like a hint?" she asks. "Parameters can have defaults. And a function can describe itself."

Rocket fixes both problems in two lines.

Default values

A parameter can have a default, written with = in the def line. If the call leaves that argument out, the default is used. Parameters with defaults go after the ones without.

You can also name an argument when you call, like reserve=0. That is a keyword argument. It lets you skip the ones in the middle.

# fuel_needed.py
def fuel_needed(distance, rate=3, reserve=10):
    """Fuel for a trip: distance times rate, plus a reserve."""
    return distance * rate + reserve

print(fuel_needed(20))
print(fuel_needed(20, 4))
print(fuel_needed(20, reserve=0))
70
90
60
CalldistanceratereserveResult
fuel_needed(20)203 (default)10 (default)70
fuel_needed(20, 4)20410 (default)90
fuel_needed(20, reserve=0)203 (default)060
What does fuel_needed(10) return?
What does fuel_needed(10, 2, 5) return?
What does fuel_needed(5, reserve=20) return?

Docstrings

The line in triple quotes right under def is a docstring. It says what the function does. Unlike a comment, Python keeps it, and help() shows it.

Try help(fuel_needed) in the shell after running the file. You will see the def line and the docstring.

DOCSTRINGS
  • Read the question.
  • Tap your answer.
Where does a docstring go?
At your computer
1. Type fuel_needed.py, save it and run it. Check 70, 90 and 60.
2. In the shell, type help(fuel_needed) and press Enter.
3. Add print(fuel_needed(10, 2, 5)) and check your answer.
4. Add a docstring to the to_minutes function from yesterday.

Practice: a default for text

Defaults work for strings too. Here the greeting has a default, but a caller can still choose a different one.

# greet.py
def greet(call_sign, greeting="Welcome aboard"):
    """Make a greeting for a crew call sign."""
    return f"{greeting}, {call_sign}!"

print(greet("Comet"))
print(greet("Pathfinder", "Good morning"))
print(greet(greeting="Hello", call_sign="Nova"))
Welcome aboard, Comet!
Good morning, Pathfinder!
Hello, Nova!
READ GREET.PY
  • Read the question.
  • Tap your answer.
Why does the first line start with Welcome aboard?
How does the last call get its order right?
Which def line is not allowed?
What does greet("Orbit") return?
What does greet("Orbit", "Hi") return?
def area(w, h=2): return w * h. What is area(5)?
StatementTrue or false?
A docstring goes in triple quotes under the def line.?
help() can show a function's docstring.?
A default is used even when the caller gives that argument.?
Keyword arguments let you pass values by name.?
WHY THIS EXERCISEDefaults step in only for missing arguments, and keyword arguments match by name.

Defaults keep calls short, and docstrings keep your future self informed. Tomorrow you will move your functions into a module of your own.

← Wednesday