Python Functions
Estimated reading: 3 minutes 22 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Positional-Only & Keyword-Only Arguments

Or Copy Link

CONTENTS
Scroll to Top