Skip to content

Arguments & Parameters

Python’s argument system is one of the most flexible in any programming language. It allows you to build APIs that are both descriptive and easy to use. However, with this flexibility comes responsibility—understanding how Python maps arguments to parameters is critical for writing bug-free code.


By default, Python maps arguments to parameters based on their position.

def power(base, exponent):
return base ** exponent
power(2, 3) # base=2, exponent=3

You can ignore the order by explicitly naming the parameters. This makes code significantly more readable.

power(exponent=3, base=2)

Parameters can have default values. If the caller omits the argument, the default is used.


Sometimes a function needs to handle an unknown number of inputs.

  • *args (Positional): Collects extra positional arguments into a Tuple.
  • **kwargs (Keyword): Collects extra keyword arguments into a Dictionary.
variadic.py
def setup_user(name, *roles, **metadata):
print(f"User: {name}")
print(f"Roles: {roles}") # Tuple
print(f"Metadata: {metadata}") # Dictionary
setup_user("Alice", "Admin", "Editor", theme="dark", lang="en")

In modern Python (3.8+), you can force callers to use specific argument styles.

Arguments before the / must be positional. This is common in low-level library functions.

def incr(x, /):
return x + 1
# incr(x=5) # TypeError!

Arguments after the * must be passed as keywords. This is excellent for ensuring clarity in functions with many parameters.

def send_email(to, *, subject, body):
pass
# send_email("a@b.com", "Hello", "Body") # TypeError!
send_email("a@b.com", subject="Hello", body="Body") # Correct

You can “explode” a list or dictionary into individual function arguments using the same * and ** operators.

unpacking.py
data = {"subject": "Alert", "body": "Disk full"}
send_email("admin@server.com", **data) # Unpacks dict into keywords
SyntaxNameUse Case
namePositional/KeywordStandard parameter.
name=valDefaultOptional parameter.
*argsVariadic PositionalHandle any number of items.
**kwargsVariadic KeywordHandle any number of settings/flags.
/Positional-Only MarkerForce no keyword use.
*Keyword-Only MarkerForce explicit keywords for clarity.