functools.Placeholder reserves a positional slot inside a partially applied callable.
Make this comfortable
Python partial Placeholder: leave a positional amount open
Operation contract
A quote function expects an amount, a fixed fee, and a currency. The partial binds the latter two while leaving the amount open, so a caller can price a forty-seven-unit invoice with a seven-unit fee. A call that supplies no amount fails before the quote function runs.
Failure boundary
Every placeholder must receive a positional value at call time. Placeholder cannot stand in for a keyword argument. A partial object binds values but does not validate whether the fee, currency, or amount matches a live pricing policy; that belongs in the called function or its owner.
Working program
from functools import Placeholder, partial
def invoice_balance(amount, fixed_fee, currency):
return f"{currency} {amount - fixed_fee}"
quote_after_fee = partial(invoice_balance, Placeholder, 7, "INR")
print("balance", quote_after_fee(47))
try:
quote_after_fee()
except TypeError:
print("missing_amount_rejected", True)Output
balance INR 40
missing_amount_rejected TrueCosts and limits
A partial stores bound argument references and adds a small call wrapper. It does not precompute the invoice result or clone mutable bound objects.
Common Mistakes
- All reserved positional slots must be filled.
- Placeholder is not supported as a keyword value in partial.
- Binding an amount or fee does not enforce a business rule.
Connected lessons
python
partial-placeholder-position
