Naming Small Functions With Arrows

The quote form can now work out a fee and a turnaround, and fill in the defaults for anything a client leaves blank. What it can’t do yet is write the reply. The translator’s Python script has two tiny formatters for that: one for the emails she sends clients, with a euro sign and a thousands separator, and one for her bookkeeping sheet, with the bare number.

Each formatter is a whole def for one line of work. In JavaScript, you can write small functions like these as arrows, but they aren’t quite the shortcut a Pythonista expects. Start with the formatters as they stand in Python.

Port the Formatters as Arrows

Here are the two formatters and the quote lines that use them:

Language: Python
def for_email(amount):
    return f"€{amount:,.2f}"

def for_sheet(amount):
    return f"{amount:.2f}"

print(f"Bauer Legal: {for_email(1116)}, 2 working days")
print(f"Nordwind GmbH: {for_sheet(1128)}, 6 working days")

If those def blocks feel long for one expression each, you might think of a lambda, such as for_sheet = lambda amount: f"{amount:.2f}". PEP 8 tells you not to do that. Use a lambda when passing a function, such as a sort key, and use def for a function you name.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.