π·οΈ 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 :
