Giving Parameters Defaults
In this lesson, you gave the translator’s JavaScript functions their defaults and saw why
Python’s None idiom doesn’t carry over.
The quote form sends only the options a client ticked, so most calls leave something out. The notes on a quote work the same way: the first note starts a quote’s list, so that call passes only the note, and later notes pass the list back in to add to it. Here’s the Python helper:
def add_note(note, notes=None):
if notes is None:
notes = []
notes.append(note)
return notes
print(add_note("rush job"))
It gives notes a default of None and starts a fresh list inside. That’s the idiom because
Python builds a default once, when the def line runs, so a list default would be shared by every
call.
The line-by-line port turns None into null and is into ===, assuming null is
JavaScript’s None. It drops the =None, since a JavaScript call can already leave any argument
out: