When calling a function in Python, there are two ways to pass arguments: “Positional Arguments” and “Keyword Arguments.”
For simple functions, positional arguments are sufficient. However, when a function has many arguments or complex configuration items, using keyword arguments can dramatically improve code readability. This article explains these two methods and the rules for combining them.
1. Positional Arguments
Positional arguments are the most basic method, where values are passed in the order defined in the function. As an example, let’s create a function that calculates hotel accommodation fees.
def calculate_hotel_fee(room_price, nights, service_fee, tax_rate):
"""
Function to calculate accommodation fees
"""
subtotal = (room_price * nights) + service_fee
total = int(subtotal * (1 + tax_rate))
return total
# Calling with positional arguments
# Order of arguments: room price, nights, service fee, tax rate
payment = calculate_hotel_fee(12000, 3, 2000, 0.1)
print(f"Total Payment: {payment} yen")
Output:
Total Payment: 41800 yen
The disadvantage of this method is that just looking at the calling code calculate_hotel_fee(12000, 3, 2000, 0.1) does not intuitively tell you what each number means (which is the service fee? which is the tax rate?). Also, if you mistake the order, you will get an incorrect calculation result.
2. Keyword Arguments
Keyword arguments are a method of explicitly passing values in the format argument_name=value when calling a function. Using this method, you do not need to worry about the order of arguments, and the meaning of the code becomes clear.
# Calling with keyword arguments
# Works correctly even if the order is changed
payment_kw = calculate_hotel_fee(
nights=3,
room_price=12000,
tax_rate=0.1,
service_fee=2000
)
print(f"Total Payment (Keyword): {payment_kw} yen")
Output:
Total Payment (Keyword): 41800 yen
Since it is written as nights=3 or tax_rate=0.1, the intention is immediately conveyed to anyone reading the code. The use of keyword arguments is recommended for functions with many arguments.
3. Mixing Positional and Keyword Arguments
In Python, it is possible to mix positional arguments and keyword arguments in a single function call. However, there is one important rule: “Positional arguments must be written first, and keyword arguments must be written afterwards.”
# Correct example of mixing
# Pass the first two as positional arguments, the rest as keyword arguments
payment_mixed = calculate_hotel_fee(12000, 3, tax_rate=0.1, service_fee=2000)
print(f"Total Payment (Mixed): {payment_mixed} yen")
Error Case
If you write positional arguments after keyword arguments, a SyntaxError occurs.
# Incorrect example of mixing
# You cannot write a positional argument (2000) after a keyword argument
# payment_error = calculate_hotel_fee(12000, nights=3, 2000, 0.1)
# SyntaxError: positional argument follows keyword argument
Summary
- Positional Arguments: Pass values in the defined order. The code is short, but the meaning of arguments can be hard to understand.
- Keyword Arguments: Pass values as
argument_name=value. Order does not matter, and readability is high. - Mixing: Possible by writing positional arguments first and keyword arguments later.
Especially when there are many arguments or when you want to specify only some of the arguments that have default values, actively using keyword arguments makes the code easier to read and maintain.
