Python Functions
Estimated reading: 3 minutes 264 views

Python Positional-Only and Keyword-Only Arguments – Modern Function Control

Introduction – Why These Argument Types Matter

In modern Python (especially Python 3.8+), functions can be more precise in how they accept arguments using positional-only and keyword-only parameters.

This helps:

  • Prevent ambiguity and bugs in large codebases
  • Enforce clear usage patterns
  • Make APIs and libraries more intuitive

By explicitly defining how parameters must be passed, developers can write cleaner and safer functions.

In this guide, you’ll learn:

  • What are positional-only and keyword-only arguments
  • How to define them using / and *
  • Examples and real-world usage
  • Best practices and common mistakes

Positional-Only Arguments (Using /)

A parameter defined before / in a function must be passed by position, not by name.

Syntax:

def func(a, b, /, c, d):
    # a and b are positional-only

Example:

def multiply(a, b, /):
    return a * b

print(multiply(3, 4))     #  Valid
# print(multiply(a=3, b=4))   TypeError

Tip: Use when argument names shouldn’t be exposed (like len() or abs() in Python).


Keyword-Only Arguments (Using *)

A parameter defined after * must be passed by keyword, not position.

Syntax:

def func(a, *, b, c):
    # b and c are keyword-only

Example:

def greet(name, *, message="Hello"):
    print(f"{message}, {name}!")

greet("Alice", message="Welcome")  # 
# greet("Alice", "Welcome")         TypeError

Use Case: Helpful when your function has many optional parameters or booleans.


Mixing All Types Together

def complex_func(a, b, /, c, *, d, e=10):
    print(a, b, c, d, e)

Example:

complex_func(1, 2, 3, d=4)  #  Correct
# complex_func(a=1, b=2, c=3, d=4)   TypeError

Breakdown:

  • a, b: Positional-only
  • c: Can be passed either way
  • d, e: Keyword-only

Common Mistakes

MistakeSolution
Using / or * in Python < 3.8These features are only available in 3.8+
Mixing argument types without structureAlways place / and * carefully
Forgetting to use keyword for keyword-onlyUse argument_name=value format

Best Practices

  • Use positional-only when internal logic doesn’t care about argument names
  • Use keyword-only for optional, configurable, or boolean parameters
  • Add / and * to public APIs to enforce clean usage
  • Always document argument behavior when mixing them

Summary – Key Takeaways

  • / indicates all parameters before it are positional-only
  • * means all parameters after it are keyword-only
  • These improve function clarity, robustness, and API consistency
  • Supported only in Python 3.8 and above

FAQ Section

What is the purpose of / in function arguments?

It makes all parameters before it positional-only—they can’t be passed using keywords.

Why would I use keyword-only arguments?

To prevent accidental misuse and to make function calls clearer, especially when parameters are optional or configurable.

Can I use both / and * in one function?

Yes. This allows mixing positional-only, flexible, and keyword-only arguments.

Are these features backwards compatible?

No. / and * syntax in parameter lists requires Python 3.8+.

Do built-in functions in Python use positional-only arguments?

Yes, many do (like len(), abs(), etc.), but it wasn’t user-definable until Python 3.8.


Share Now :
Share

Python Positional-Only & Keyword-Only Arguments

Or Copy Link

CONTENTS
Scroll to Top