Sub topic 11
Controlling how callers pass arguments with / and *
This chapter assumes you can define a function with def, you know positional and keyword arguments, and you have seen default values. Here you will learn two markers, / and *. You can put them in a parameter list to limit how callers pass each argument.
Why restrict how arguments are passed
By default, a caller can pass any parameter by position or by name. That freedom has a cost. Once callers use a parameter name as a keyword, renaming it breaks their code. Likewise, a call like clamp(150, 0, 120) is hard to read, because nothing says what 120 means.
The two markers let you decide, parameter by parameter, which calling styles are allowed.
The two markers
Python reads a parameter list left to right. A bare / or * is not a parameter. It is a divider that splits the list into zones.
def clamp(value, /, low=0, *, high=100):
return max(low, min(value, high))
print(clamp(150)) # 100
print(clamp(-5)) # 0
print(clamp(150, high=120)) # 120The list has three zones:
- Before
/: positional-only.valuemust be passed by position. - Between
/and*: either style.lowcan be passed by position or by name. - After
*: keyword-only.highmust be passed by name.
What happens when a caller breaks the rules
Breaking a restriction raises TypeError. This program shows both failures:
def clamp(value, /, low=0, *, high=100):
return max(low, min(value, high))
try:
clamp(value=150) # value is positional-only
except TypeError as error:
print("TypeError:", error)
try:
clamp(150, 0, 120) # high is keyword-only
except TypeError as error:
print("TypeError:", error)Both calls fail before the function body runs. That early failure is the point. The mistake shows up at the call site and does not turn into a wrong answer later.
When to use each marker
Use / when the parameter name carries no meaning for callers or may change later. Built-in functions such as len(obj) work this way. If value is positional-only, you can rename it to number without breaking anyone.
Use * when a call would be unclear without a label. Boolean flags and numeric limits are typical cases. clamp(150, high=120) reads better than a bare 120.
You can use either marker alone. A function can have a * with no /, or the reverse.
Common errors
- Writing
/after*. The/must come first, sodef f(a, *, b, /)is a syntax error. - Putting a parameter with a default before one without a default in the positional zone. The usual rule still applies, for example
def f(a=1, b)is invalid. Keyword-only parameters are exempt, sodef f(a, *, b, c=2)anddef f(*, a=1, b)are both legal. - Passing a positional-only name as a keyword when you also accept
**kwargs. In that case the name goes intokwargsand does not clash with the positional parameter.
Exercise
Write a function repeat(text, /, times=2, *, sep="") that returns text repeated times times, joined by sep. Which of these calls raise TypeError: repeat("ab"), repeat(text="ab"), repeat("ab", 3, sep="-"), repeat("ab", 3, "-")?
Show answer
Use return sep.join([text] * times). The call repeat("ab") returns "abab". repeat("ab", 3, sep="-") returns "ab-ab-ab". Two calls raise TypeError. repeat(text="ab") fails because text is positional-only. repeat("ab", 3, "-") fails because sep is keyword-only.