How can we force naming of parameters when calling a function?
Codemia Knowledge Hub ·
Introduction
Forcing callers to name arguments can make APIs clearer and safer, especially when several parameters have similar types or meanings. In Python, the direct tool for this is keyword-only parameters. Instead of relying on convention alone, you can design the function signature so positional calls are rejected automatically.
Force names with keyword-only parameters
In Python, put * before the first parameter that must be passed by name.
This works because host, port, and timeout are keyword-only.
A positional call fails:
That is the standard Python answer when you want to force named arguments at the call site.
Mix positional and keyword-only parameters
Sometimes only part of the signature needs forced naming. Python allows a hybrid design.
Here image_path may be positional, while the configuration parameters must be named. This is often a good balance between convenience and readability.
Why this improves API quality
Named arguments help when values are easy to mix up:
Compare that with a positional-only style such as create_user("Mina", 30, True). The named version is more self-documenting and less likely to be called incorrectly months later.
This matters most when:
- Parameters have the same type
- Several optional flags exist
- Wrong order can still produce syntactically valid code
Enforcing names without * is usually the wrong route
You could manually inspect *args and **kwargs and raise your own errors, but that is unnecessary when the language already provides keyword-only parameters.
The built-in signature mechanism gives you:
- Better error messages
- Cleaner function definitions
- Better tooling support
- More accurate introspection
Use the language feature first. Manual argument policing is rarely an improvement here.
Compare with positional-only parameters
Python also supports the opposite concept: positional-only parameters, marked with /.
That means callers cannot write ratio(x=10, y=2). It is useful to know because it shows that Python signatures can express both sides of the contract:
- Parameters that must be positional
- Parameters that must be named
For this question, keyword-only parameters are the tool you want.
Common Pitfalls
The biggest mistake is assuming that default arguments automatically imply named arguments. They do not. A parameter can have a default value and still be positional unless you make it keyword-only.
Another issue is overusing forced naming for trivial APIs. If the function takes one obvious value, requiring a keyword may add noise instead of clarity.
Developers also sometimes write custom argument-validation code when a simple * in the function signature would solve the problem directly.
Finally, be intentional about API evolution. Adding keyword-only parameters can improve readability, but changing an old positional API may break callers if done without planning.
Summary
- In Python, force named arguments with keyword-only parameters.
- Put
*before the parameters that must be passed by keyword. - Mix positional and keyword-only parameters when that makes the API clearer.
- Prefer signature-level enforcement over manual
argsandkwargschecking. - Use forced naming where it improves clarity, not just because it is possible.
Related reading
- How could I use aws lambda to write file to s3 python?
- How could I use requests in asyncio?
- How did Python implement the built-in function pow?
- How do I abort the execution of a Python script?
- How do I access the ith column of a NumPy multidimensional array?
- How do I add a path to PYTHONPATH in virtualenv
- How do I add a placeholder on a CharField in Django?
- How do I add an extra column to a NumPy array?
Course lessons on this
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.