This guide explains positional-only parameters with / through a focused Python example. You will learn the behavior to expect, why it matters in a real program, and how to recognize the common mistakes before applying it in your own project.
What You Will Learn
The / marker in a signature makes preceding parameters positional-only. Built-in functions often use it, and it can keep implementation parameter names out of an API contract.
When and Why to Use It
The following example focuses on a practical use of Positional-Only Parameters with /.
Step-by-Step Example
def calculate_area(width, height, /, *, unit="cm2"):
return f"{width * height} {unit}"
print(calculate_area(4, 6))
print(calculate_area(4, 6, unit="m2"))
Understanding the Example
width and height must be passed by position; unit must be passed by keyword. The displayed values are 24 cm2 and 24 m2.
Expected Output
24 cm2
24 m2
Important Behavior and Edge Cases
The slash appears only in the function definition, never as an argument at call time. Using width=4 would raise TypeError.
Try It Yourself
Define a distance function with positional-only coordinates and a keyword-only unit.
Common Beginner Mistakes
- The slash appears only in the function definition, never as an argument at call time. Using width=4 would raise TypeError.
- Confusing a returned value with printed output, or failing to verify a function’s behavior with different input values.
- Skipping the practice step and assuming the example covers every input. Test at least one normal case and one edge case relevant to this topic.
Key Takeaways
- The / marker in a signature makes preceding parameters positional-only.
- width and height must be passed by position; unit must be passed by keyword.
- Apply the technique deliberately, checking its documented input requirements and failure behavior before integrating it into a larger project.