Python Function Parameter Kinds
Learn positional-only, keyword-only, variable positional, and variable keyword parameters and how they shape a function calling contract.
Intermediate
Python functions can control how callers provide arguments. Beyond ordinary positional and keyword arguments, a function can require some arguments by position, require others by name, or accept a variable number of positional or keyword arguments.
Positional-only parameters
A slash in a function definition marks the parameters before it as positional-only. Callers must provide those arguments by position.
def divide(total, count, /):
return total / count
print(divide(12, 3))Keyword-only parameters
A bare asterisk marks the parameters after it as keyword-only. This makes calls explicit when an option is easier to understand by name.
def connect(host, *, timeout=5):
return f"{host} (timeout={timeout})"
print(connect("example.com"))
print(connect("example.com", timeout=10))Variable positional arguments
A parameter written with one asterisk collects remaining positional arguments into a tuple. The name is only a convention.
def total(*numbers):
return sum(numbers)
print(total(2, 4, 6))
print(total())Variable keyword arguments
A parameter written with two asterisks collects remaining keyword arguments into a dictionary. The name kwargs is only a convention.
def describe(**details):
for name, value in details.items():
print(f"{name}: {value}")
describe(language="Python", level="core")Combining parameter kinds
A function can combine positional-only parameters, ordinary parameters, keyword-only parameters, and variable arguments. Python constrains the order so the signature remains unambiguous.
def report(name, /, *values, unit="items", **options):
print(name, values, unit, options)
report("sales", 10, 20, unit="rows", year=2026)Why parameter kinds matter
Parameter kinds are part of a function API contract. Use them when they make the intended calling style clearer or protect an interface from accidental dependence on parameter names.
def format_price(amount, /, *, currency="USD", places=2):
return f"{amount:.{places}f} {currency}"
print(format_price(12.5))
print(format_price(12.5, currency="EUR", places=0))Key takeaways
- Parameters before / are positional-only.
- Parameters after a bare * are keyword-only.
- *args collects extra positional arguments into a tuple.
- **kwargs collects extra keyword arguments into a dictionary.
- args and kwargs are conventions; the * and ** syntax supplies the behavior.
- Use parameter kinds to make function interfaces deliberate and readable.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.