Passing Options by Name
In this lesson, you saw what a Python keyword call does when it’s ported to JavaScript, and passed options by name the JavaScript way, with one object.
Python’s quote_line() passes rush on by name, quote(words, rush=rush), and its caller writes
quote_line("Bauer Legal", 6200, rush=True). Here’s the translator’s Python script:
def quote(
words,
rate=0.12,
rush=False,
minimum=40,
):
fee = max(words * rate, minimum)
days = 2 + words // 2000
if rush:
return fee * 1.5, max(1, days // 2)
return fee, days
def for_email(amount):
return f"€{amount:,.2f}"
def quote_line(
client,
words,
rush=False,
fmt=for_email,
):
fee, days = quote(words, rush=rush)
return f"{client}: {fmt(fee)}, {days} working days"
print(quote_line("Bauer Legal", 6200, rush=True))
The script prints Bauer Legal: €1,116.00, 2 working days. The line-by-line port keeps both keyword
calls: