Category: python
Positional-only Parameters
Published on 27 Oct 2026
Explanation
Parameters before / are positional-only and cannot be supplied by keyword.
Code:
def divide(a, b, /): return a / b print(divide(10, 2))
Explanation
Parameters after a standalone * must be passed by keyword.
Code:
def connect(host, *, timeout=30): print(host, timeout)
connect('localhost', timeout=10)
Explanation
A signature can combine positional-only, regular, and keyword-only parameters.
Code:
def create_user(name, /, role, *, active=True):
print(name, role, active)
create_user('Arun', 'Developer', active=False)
Explanation
Keyword-only arguments make optional settings explicit and reduce mistakes caused by argument order.
Code:
def fetch(url, *, timeout=10, retries=3):
print(url, timeout, retries)
fetch('https://example.com', timeout=5)
Explanation
Use parameter restrictions when they make an API easier to understand or maintain.
Code:
def price(amount, quantity, /, *, tax=0.0):
return amount * quantity * (1 + tax)
print(price(100, 2, tax=0.18))