Python Keyword Arguments – Write Clear and Flexible Functions
Introduction – Why Keyword Arguments Matter
When calling functions in Python, we often use positional arguments. But in many real-world scenarios—like APIs, configuration options, or user input—order shouldn’t matter.
That’s where keyword arguments shine.
They allow you to call a function by explicitly naming parameters, improving readability and making arguments order-independent.
In this guide, you’ll learn:
- What keyword arguments are and how to use them
- Differences from positional arguments
- How to mix positional and keyword arguments
- Real-world examples and best practices
Syntax of Keyword Arguments
def function_name(param1, param2):
# code
function_name(param1=value1, param2=value2)
Key Rule: Parameter names must match those in the function definition.
Example 1: Simple Keyword Arguments
def introduce(name, age):
print(f"My name is {name} and I am {age} years old.")
introduce(age=25, name="Alice")
Output:
My name is Alice and I am 25 years old.
Explanation:
Using keywords lets you pass arguments in any order.
Positional vs Keyword Arguments
| Feature | Positional Arguments | Keyword Arguments |
|---|---|---|
| Order matters | Yes | No |
| Readability | Medium | High |
| Flexibility | Less flexible | Highly flexible |
| Common in | Short functions | Config-heavy or multi-parameter funcs |
Mixing Positional and Keyword Arguments
Example 2:
def book_flight(from_city, to_city, airline="Delta"):
print(f"Flight from {from_city} to {to_city} via {airline}")
book_flight("Paris", to_city="New York")
Rule:
Positional arguments must come before keyword arguments.
This is valid:
func(10, b=5)
This is invalid:
func(a=10, 5) # SyntaxError
Real-World Example: Sending Email
def send_email(to, subject, body="(No Content)", cc=None):
print(f"To: {to}")
print(f"Subject: {subject}")
print(f"Body: {body}")
print(f"CC: {cc or 'None'}")
send_email(subject="Update", to="user@example.com", cc="boss@example.com")
Use Case: Keyword arguments are perfect when calling functions with many optional parameters.
Common Mistakes
| Mistake | Fix |
|---|---|
| Mixing keyword and positional wrongly | Positional args must come first |
| Misspelling parameter names | Match names exactly as defined in the function |
| Using reserved keywords as names | Avoid using Python keywords as parameter names |
Summary – Key Takeaways
- Keyword arguments let you assign values by name, not position
- You can mix positional and keyword arguments (positional first)
- Ideal for optional and named parameters
- Keyword arguments improve readability and flexibility
FAQ Section
Can keyword arguments be in any order?
Yes, as long as parameter names match, order doesn’t matter.
Can I use only keyword arguments?
Yes. You can call a function with all keyword arguments:
introduce(name="Bob", age=30)
What happens if I misspell a keyword?
Python will raise a TypeError:
TypeError: got an unexpected keyword argument
Can keyword arguments be combined with default arguments?
Yes. They often go hand-in-hand for better flexibility.
Are keyword arguments required?
No. They are optional unless specified using special syntax like *, (Python 3.8+).
Share Now :
