Introduction: Combining Function Parameters in Python
A function may need to handle various scenarios, and for this purpose, it may need to accept different types of input. Combining function parameters in Python provides a way to handle these different input requirements. These input requirements can vary depending on the scenario.
To handle these different requirements, Python allows several parameter types and special syntax to be combined:
- positional-only parameters
- regular parameters
- keyword-only parameters
*args**kwargs
This page explains how these parameter types can be combined, the order they must follow in a function definition and how default values work with them.
Combining Different Parameter Types in Python – Part 1 [Beginner]
A function can use different parameter types in the same definition. Python requires these parameters to appear in a specific order.
When combining function parameters in Python, each parameter follows its own passing rule.
The main parameter types used in these combinations are positional-only, regular, and keyword-only parameters.
Type 1: Positional-Only and Regular Parameters
You can place regular parameters after positional-only parameters. The / marks where the positional-only parameters end.
Example
def introduce(first_name, /, last_name):
print(first_name, last_name)
introduce("Alice", "Smith")
introduce("Alice", last_name="Smith")
Explanation: Here, first_name is positional-only because it appears before /. The last_name parameter is regular, so it can receive a value either by position or by keyword.
Error: Passing a Positional-Only Parameter by Keyword
The following call to the same function incorrectly passes the positional-only parameter first_name by keyword.
introduce(first_name="Alice", last_name="Smith")
# Error:
TypeError
Explanation: This function call fails because first_name is positional-only.
Type 2: Regular and Keyword-Only Parameters
You can place keyword-only parameters after regular parameters. Use * to mark where the keyword-only parameters begin.
Example
def introduce(name, age, *, city):
print(name, age, city)
introduce("Alice", 25, city="Delhi")
Explanation: Here, name and age are regular parameters. The city parameter is keyword-only.
The regular parameters can be passed by position or by keyword, but city must be passed by keyword.
Type 3: Positional-Only, Regular and Keyword-Only Parameters
Python also lets you combine all three types in one function.
Example
def introduce(first_name, /, last_name, *, age):
print(first_name, last_name, age)
introduce("Alice", "Smith", age=25)
Explanation: The parameters work as follows:
first_nameis positional-only.last_nameis regular.ageis keyword-only.
The first value is passed by position, the second value can be passed by position, and age must be passed by keyword.
Type 4: Understanding the Complete Parameter Order
When these parameter types are combined, Python requires them to appear in a specific order in the function definition. From left to right, the order is:
positional-only, /, regular, *, keyword-only
- Parameters before
/are positional-only. - Parameters between
/and*are regular. - Parameters after
*are keyword-only.
For example:
def func(a, /, b, *, c):
pass
ais positional-only./marks the end of positional-only parameters.bis regular.*marks the beginning of keyword-only parameters.cis keyword-only.
Explanation: The symbols / and * define parameter rules. They are not parameters themselves.
Default Values With Different Parameter Types
A default value is separate from a parameter’s passing rule. A parameter can be regular, positional-only, or keyword-only and can also have a default value.
The symbols / and * decide how a parameter must be passed. The = sign gives the parameter a default value.
The following subtopics show these rules in practice.
Regular Parameters With Default Values
A regular parameter can have a default value. The argument can be passed by position or by keyword.
def greet(name="Guest"):
print("Hello,", name)
greet()
greet("Alice")
greet(name="Bob")
# Output:
Hello, Guest
Hello, Alice
Hello, Bob
Explanation: Here, name is a regular parameter with the default value "Guest".
Positional-Only Parameters With Default Values
A positional-only parameter can also have a default value. The / still makes the parameter positional-only.
def greet(name="Guest", /):
print("Hello,", name)
greet()
greet("Alice")
# Output:
Hello, Guest
Hello, Alice
Explanation: Here, name="Guest" has two properties: it is a positional-only parameter and it has a default value.
The default value does not change the parameter into a regular parameter.
Error: Passing a Positional-Only Parameter by Keyword
The following function call shows that name must still be passed by position even though it has a default value.
greet(name="Alice")
# Error:
TypeError
Explanation: This function call fails because a is positional-only.
Keyword-Only Parameters With Default Values
A keyword-only parameter can have a default value. This allows the function to provide a value automatically when the caller does not supply that parameter.
def greet(*, name="Guest"):
print("Hello,", name)
greet()
greet(name="Alice")
# Output:
Hello, Guest
Hello, Alice
Explanation: Here, name is keyword-only because it appears after *. It also has the default value "Guest".
The default value allows the caller to omit the argument, but the parameter must still use its keyword when a value is supplied.
Comparing Default Values Across Parameter Types
| Parameter | Parameter Type | Default | Valid Call |
|---|---|---|---|
name="Guest" |
Regular | "Guest" |
greet("Alice") |
name="Guest", / |
Positional-only | "Guest" |
greet("Alice") |
*, name="Guest" |
Keyword-only | "Guest" |
greet(name="Alice") |
The default value does not determine the parameter type. The parameter’s position relative to / and * determines how it must be passed.
Combining Multiple Parameter Types With Default Values
You can give default values to parameters from different parameter types in the same function.
Example
def func(a=10, /, b=20, *, c=30):
print(a, b, c)
func()
func(1)
func(1, 2)
func(1, 2, c=3)
# Output:
10 20 30
1 20 30
1 2 30
1 2 3
Explanation: Here:
a=10is positional-only with a default value.b=20is regular with a default value.c=30is keyword-only with a default value.
Each parameter keeps its own passing rule even though all three have default values.
How / and * Change the Parameter Type
The same parameter name and default value can have different parameter types depending on where it appears in the function definition.
Regular Parameter With a Default Value
def func(a=10):
passExplanation: Here, a is a regular parameter with a default value.
Positional-Only Parameter With a Default Value
Adding / changes a from a regular parameter to a positional-only parameter.
def func(a=10, /):
passExplanation: Here, a is a positional-only parameter with a default value.
Keyword-Only Parameter With a Default Value
Adding * changes a to a keyword-only parameter.
def func(*, a=10):
passExplanation: Here, a is a keyword-only parameter with a default value.
The value 10 does not determine the parameter type. The placement of a relative to / and * determines the type.
Combining Different Parameter Types in Python – Part 2 [Intermediate]
Part 2 extends the parameter combinations covered in Part 1 by showing how *args and **kwargs can be used when combining function parameters in Python.
These parameter types have different jobs:
- Positional-only parameters must be passed by position.
- Regular parameters can be passed by position or keyword.
*argscollects extra positional arguments.- Keyword-only parameters must be passed by keyword.
**kwargscollects extra keyword arguments.
The following combinations show how these parameter types work together:
- Regular +
*args *args+ Keyword-Only Parameters- Regular +
*args+**kwargs - Positional-Only +
*args+ Keyword-Only - Positional-Only + Regular +
*args+ Keyword-Only +**kwargs - Complete Parameter Order
Regular + *args
A regular parameter can appear before *args. The regular parameter receives its normal argument, while *args collects additional positional arguments.
Example
def show_values(name, *args):
print("Name:", name)
print("Other values:", args)
show_values("Alice", 10, 20, 30)
# Output:
Name: Alice
Other values: (10, 20, 30)Explanation: Here, name is a regular parameter. The additional positional arguments are collected in args.
*args + Keyword-Only Parameters
Parameters written after *args are keyword-only parameters. This is an important difference from using a standalone *.
Example
def show_values(*args, total):
print("Values:", args)
print("Total:", total)
show_values(10, 20, 30, total=60)
# Output:
Values: (10, 20, 30)
Total: 60Explanation: Here, args collects the positional arguments. The total parameter appears after *args, so it is keyword-only.
Error: Passing a Keyword-Only Parameter by Position
The following call incorrectly passes the total parameter by position instead of using its keyword.
show_values(10, 20, 30, 60)
# Error:
TypeErrorExplanation: The value 60 is treated as another positional argument and is collected by *args. The required total keyword-only parameter is therefore not supplied.
Regular + *args + **kwargs
A function can use a regular parameter, *args, and **kwargs together.
Example
def show_values(name, *args, **kwargs):
print("Name:", name)
print("Extra positional:", args)
print("Extra keyword:", kwargs)
show_values(
"Alice",
10,
20,
age=25,
city="Delhi"
)
# Output:
Name: Alice
Extra positional: (10, 20)
Extra keyword: {'age': 25, 'city': 'Delhi'}Explanation: Here, name receives the first positional argument. The remaining positional arguments go into args. The extra keyword arguments go into kwargs.
Positional-Only + *args + Keyword-Only
You can also combine positional-only parameters with *args and keyword-only parameters.
Example
def func(a, /, *args, c):
print("a:", a)
print("args:", args)
print("c:", c)
func(10, 20, 30, c=40)
# Output:
a: 10
args: (20, 30)
c: 40Explanation: Here,
ais positional-only.*argscollects additional positional arguments.cis keyword-only.
The c parameter is keyword-only because it appears after *args.
Positional-Only + Regular + *args + Keyword-Only + **kwargs
Python allows all five parameter types in one function. This combination is useful when a function needs strict control over some arguments while still accepting additional arguments.
Example
def func(a, /, b, *args, c, **kwargs):
print("a:", a)
print("b:", b)
print("args:", args)
print("c:", c)
print("kwargs:", kwargs)
func(
10,
20,
30,
40,
c=50,
color="blue",
active=True
)
# Output:
a: 10
b: 20
args: (30, 40)
c: 50
kwargs: {'color': 'blue', 'active': True}Explanation: Each part has a different job:
ais positional-only.bis regular.*argscollects additional positional arguments.cis keyword-only.**kwargscollects additional keyword arguments.
The call follows the same order. The first positional value goes to a. The second goes to b. The remaining positional values go to args. The c=50 argument goes to the keyword-only parameter, while the remaining keyword arguments go into kwargs.
Complete Parameter Order With *args and **kwargs
When combining different parameter types in Python, all five types follow a specific order:
positional-only, /, regular, *args, keyword-only, **kwargs
When *args is used, it collects additional positional arguments and also marks the beginning of keyword-only parameters. Therefore, a separate * marker is not needed.
The complete pattern can be written as:
def func(positional_only, /, regular, *args, keyword_only, **kwargs):
pass
Explanation: A function does not need to use all five types. Use only the parameter types that match the function’s requirements.
Common Mistakes When Combining Function Parameters in Python
Incorrect parameter order or argument passing can cause a SyntaxError or TypeError. These mistakes are common when several parameter types appear in one function.
- Passing a Positional-Only Parameter by Keyword
- Passing a Keyword-Only Parameter by Position
- Forgetting That Parameters After
*argsAre Keyword-Only - Placing a Required Positional Parameter After a Default
1. Passing a Positional-Only Parameter by Keyword
A positional-only parameter must be passed by position, not by keyword.
Error: Passing a Positional-Only Parameter by Keyword
The following function call incorrectly passes the positional-only parameter a by keyword.
def func(a, /):
pass
func(a=10)
# Error:
TypeError
Explanation: This function call fails because a is positional-only.
2. Passing a Keyword-Only Parameter by Position
A keyword-only parameter must be passed using its parameter name.
Error: Passing a Keyword-Only Parameter by Position
The following function call incorrectly passes the keyword-only parameter a by position.
def func(*, a):
pass
func(10)
# Error:
TypeError
Explanation: This function call fails because a is keyword-only and must be passed as a=10.
3. Forgetting That Parameters After *args Are Keyword-Only
Parameters written after *args are keyword-only. A common mistake is to pass one of these parameters by position.
Error: Passing a Parameter After *args by Position
The following function call incorrectly passes name by position.
def func(*args, name):
pass
func(10, 20, "Alice")
# Error:
TypeError
Explanation: The name parameter is keyword-only. The positional values are collected by args, so "Alice" is not assigned to name.
Correct: Passing the Keyword-Only Parameter by Keyword
The same function can be called correctly by passing name using its keyword.
func(10, 20, name="Alice")
Explanation: Here, args collects 10 and 20 as extra positional arguments, while name is passed separately as a keyword-only parameter.
4. Placing a Required Positional Parameter After a Default
For positional-only and regular parameters, a parameter without a default value cannot follow a parameter with a default value.
Error: Required Parameter After a Default
The following function definition is invalid because b is a required parameter placed after a, which has a default value.
def func(a=10, /, b):
pass
# Error:
SyntaxError
Explanation: Give b a default value or move it before the parameter with the default value.
Correct: Giving the Required Parameter a Default Value
The function becomes valid when b is also given a default value.
def func(a=10, /, b=20):
pass
Explanation: Here, both a and b have default values, so the function definition is valid. a remains positional-only, while b remains a regular parameter.
Comparing Special Parameter Syntax in Python
Python provides special parameter syntax that may look similar but serves different purposes. The following comparisons show how *, *args, and **kwargs differ.
Comparing * With *args
| Feature | Standalone * |
*args |
|---|---|---|
| Purpose | Marks the beginning of keyword-only parameters | Collects additional positional arguments |
| Collects values? | No | Yes, into a tuple |
| Example | def func(a, *, b): |
def func(a, *args): |
Example: Standalone *
def func(a, *, b):
print(a, b)
func(10, b=20)
# Output:
10 20
Explanation: Here, b is keyword-only because it appears after the standalone *.
Example: *args
def func(a, *args):
print(a, args)
func(10, 20, 30)
# Output:
10 (20, 30)
Explanation: Here, args collects the additional positional arguments into a tuple.
Comparing **kwargs With a Regular Parameter
| Feature | Regular Parameter | **kwargs |
|---|---|---|
| Purpose | Receives a specific argument | Collects additional keyword arguments |
| Collects values? | No | Yes, into a dictionary |
| Example | def func(name): |
def func(**kwargs): |
Example: Regular Parameter
def func(name):
print(name)
func(name="Alice")
# Output:
Alice
Explanation: Here, name is a specific parameter that receives the value "Alice".
Example: **kwargs
def func(**kwargs):
print(kwargs)
func(name="Alice", age=25)
# Output:
{'name': 'Alice', 'age': 25}
Explanation: Here, kwargs collects the additional keyword arguments into a dictionary.
Practical Examples: Combining Function Parameters in Python
Different parameter types can make a function easier to call and control. The following examples show combinations that can be useful in real functions.
Example 1: Required Value With Optional Settings
def send_message(message, *, priority="Normal", language="English"):
print("Message:", message)
print("Priority:", priority)
print("Language:", language)
send_message(
"Your order is ready.",
priority="High"
)
# Output:
Message: Your order is ready.
Priority: High
Language: English
Explanation: The message parameter is regular. The priority and language parameters are keyword-only and have default values.
Example 2: Positional-Only Value With Optional Details
def create_user(username, /, *, age=None, city=None):
print("Username:", username)
print("Age:", age)
print("City:", city)
create_user("alice", age=25, city="Delhi")
# Output:
Username: alice
Age: 25
City: Delhi
Explanation: Here, username must be passed by position. The optional age and city values must be passed by keyword.
Example 3: Extra Positional and Keyword Arguments
def process_data(name, *values, format="normal", **options):
print("Name:", name)
print("Values:", values)
print("Format:", format)
print("Options:", options)
process_data(
"Scores",
80,
90,
95,
format="average",
rounded=True
)
# Output:
Name: Scores
Values: (80, 90, 95)
Format: average
Options: {'rounded': True}
Explanation: Here, name is regular, values collects extra positional arguments, format is keyword-only with a default value, and options collects additional keyword arguments.
Example 4: Using All Five Parameter Types
def create_record(
record_id,
/,
name,
*tags,
status="active",
**details
):
print("ID:", record_id)
print("Name:", name)
print("Tags:", tags)
print("Status:", status)
print("Details:", details)
create_record(
101,
"Laptop",
"electronics",
"computer",
status="available",
brand="ABC",
price=55000
)
# Output:
ID: 101
Name: Laptop
Tags: ('electronics', 'computer')
Status: available
Details: {'brand': 'ABC', 'price': 55000}
Explanation: This function demonstrates the complete parameter order:
record_idis positional-only.nameis regular.*tagscollects extra positional arguments.statusis keyword-only with a default value.**detailscollects extra keyword arguments.
This combination is powerful, but it is not necessary for most functions. Use a simpler parameter list when the function does not need this level of control.
Key Takeaways: Combining Function Parameters in Python
Here are the key takeaways when combining different function parameters in Python:
- Parameters before
/are positional-only. - Regular parameters can be passed by position or by keyword.
- Parameters after a standalone
*are keyword-only. *argscollects additional positional arguments into a tuple.**kwargscollects additional keyword arguments into a dictionary.- When parameter types are combined, they must follow Python’s required parameter order.