In Python, keyword-only arguments are parameters that must be passed using their name (keyword), not by position. This means you cannot pass values normally like 10, 20; you must use name=value.
Keyword-only arguments help make your functions clear, safe, and less confusing.
🔹 What are Keyword-Only Arguments?
Keyword-only arguments are function parameters that:
Must be passed using
key=valueformat
They are defined after a special symbol * in a function.
🔹 Syntax of Keyword-Only Arguments
def function_name(*, param1, param2):
pass
🔍 Important:
-
Everything after
*becomes keyword-only -
You must use
param=valuewhen calling
🔹 Simple Example of Keyword-Only Arguments
def greet(*, name, message):
print(message, name)
greet(name="Alex", message="Hello")
🔸 Output:
Hello Alex
🔹 ❌ Wrong Usage (Positional Not Allowed)
def greet(*, name, message):
print(name, message)
greet("Alex", "Hello")
❌ This gives an error
🔍 Why?
-
After
*, arguments cannot be passed positionally
🔹 Correct Usage (Keyword Required)
def user_info(*, username, age):
print("Username:", username)
print("Age:", age)
user_info(username="John", age=25)
🔸 Output:
Username: John
Age: 25
🔹 Why Use Keyword-Only Arguments?
They are useful when:
- You want clear function calls
- You want to avoid argument confusion
- You want to force users to be explicit
🔹 Example: Payment System
def payment(*, amount, currency):
print("Amount:", amount)
print("Currency:", currency)
payment(amount=100, currency="USD")
🔸 Output:
Amount: 100
Currency: USD
🔹 Mixing Positional and Keyword-Only Arguments
You can combine normal and keyword-only arguments:
def order(product, *, price, quantity):
print(product)
print(price)
print(quantity)
order("Laptop", price=1200, quantity=2)
🔸 Output:
Laptop
1200
2
🔍 Explanation:
-
"Laptop"→ positional argument -
price,quantity→ keyword-only
🔹 Invalid Example
def order(product, *, price, quantity):
print(product, price, quantity)
order("Laptop", 1200, 2)
❌ Error occurs
🔍 Reason:
-
priceandquantitymust be written as keywords
🔹 Real-Life Example: User Registration
def register(username, *, email, country):
print(username)
print(email)
print(country)
register("alex", email="alex@gmail.com", country="Cambodia")
🔸 Output:
alex
alex@gmail.com
Cambodia
🔹 Real-Life Example: Hotel Booking System
def book_room(room_type, *, nights, price_per_night):
total = nights * price_per_night
print("Room:", room_type)
print("Total:", total)
book_room("Deluxe", nights=3, price_per_night=50)
🔹 Keyword-Only with Default Values
def profile(*, name="Guest", age=0):
print(name, age)
profile(name="John", age=30)
profile()
🔸 Output:
John 30
Guest 0
🔹 Difference Between Keyword Arguments and Keyword-Only Arguments
| Feature | Keyword Arguments | Keyword-Only Arguments |
|---|---|---|
| Order matters | No | No |
| Positional allowed | Yes | No |
Must use name=value | Optional | Required |
Defined using * | No | Yes |
🔹 Why Keyword-Only Arguments Are Important
✔ Prevents mistakes in argument order
✔ Makes code more readable
✔ Forces clarity in function calls
✔ Useful in APIs and large applications
✔ Improves code safety and structure
🔹 Key Rule to Remember
Everything after
*in function definition becomes keyword-only.
def example(a, b, *, c, d):
pass
🚀 Conclusion
Python keyword-only arguments are a powerful feature that improves code clarity and safety. They ensure that important parameters are always passed explicitly using names, reducing confusion and bugs.
They are widely used in:
- APIs
- Frameworks
- Large software systems
- Professional Python development


0 Comments