π§ 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-onlyc
: Can be passed either wayd, e
: Keyword-only
β οΈ Common Mistakes
Mistake | Solution |
---|---|
Using / or * in Python < 3.8 | These features are only available in 3.8+ |
Mixing argument types without structure | Always place / and * carefully |
Forgetting to use keyword for keyword-only | Use 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 :