Positional-Only Parameters for a Fast Path

Medium ⏱ 12 min 47% acceptance ★★★★☆ 4.4
Write a function fast_add(a, b, /) that returns a + b, where a and b are declared positional-only using the / marker, meaning they can never be passed as keyword arguments (this mirrors how many built-in CPython functions are defined for performance/clarity reasons).

Examples

Example 1
Input
fast_add(2, 3)
Output
5
Explanation

Called positionally, which is the only way allowed.

Constraints

  • Calling fast_add(a=2, b=3) must raise a TypeError, since a and b are positional-only.

Topics

Functionspositional-only parameters

Companies

IntelCisco

Hints

Hint 1

Place a bare / in the parameter list after the parameters that should be positional-only.

Hint 2

Everything before the / cannot be supplied as a keyword argument.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.