Before the / → positional argument only, thus foo(arg1=...) will raise.
Between the / and * → positional or keyword arguments
After the * → keyword only argument, thus foo(x1, x2, x3, x4, x5) will raise.
Hi, Thanks for your explanation. This is new to me!
Should it be then, raw=raw instead of just
…, *, raw, …
? Is raw the keyword and the object being passed to the keyword parameter?
Correct, it should be either maxwell_filter_prepare_emptyroom(raw_er=..., raw=...) or maxwell_filter_prepare_emptyroom(..., raw=...) because all of the arguments after the * must be provided with their keyword name (raw=, bads=, …).
Note that the variable name passed can differ, e.g. maxwell_filter_prepare_emptyroom(empty_room_raw, raw=process_raw).
@mschendel I’ve asked ChatGPT to generate an explanation.
Please explain Python’s keyword-only arguments in simple terms to somebody who’s not familiar with this concept. Please explain how to spot them and how to use them (from a user side). Please include examples of keyword-only arguments with and without default values.
Understanding Python’s Keyword-Only Arguments
In Python, keyword-only arguments are parameters that can only be passed by their name (keyword) and not by their position in the function call. These arguments provide more clarity and prevent confusion, especially when a function takes multiple parameters.
How to Spot Keyword-Only Arguments
You can identify keyword-only arguments in a function definition by looking for an asterisk (*) in the parameter list. Any parameter after the * must be passed as a keyword argument.
Using Keyword-Only Arguments
When calling a function with keyword-only arguments, you must explicitly name these arguments. This can improve readability and reduce errors.
Examples
Let’s go through examples of keyword-only arguments with and without default values.
Example 1: Keyword-Only Arguments without Default Values
In this example, greeting is a keyword-only argument. You must specify greeting when calling the function.
Example 2: Keyword-Only Arguments with Default Values
def introduce(name, *, age=None, city="Unknown"):
return f"Meet {name}, who is {age} years old and lives in {city}."
# Correct usage with default values
print(introduce("Bob")) # Output: Meet Bob, who is None years old and lives in Unknown.
# Correct usage with provided values
print(introduce("Bob", age=30, city="New York")) # Output: Meet Bob, who is 30 years old and lives in New York.
# Incorrect usage (will raise a TypeError)
# print(introduce("Bob", 30, "New York"))
In this example, age and city are keyword-only arguments with default values. You can either use the defaults or provide specific values by naming the arguments.
Summary
Keyword-only arguments are specified after an asterisk (*) in the function definition.
They must be passed by name when calling the function.
They can have default values.
They improve code clarity and reduce potential errors.
By using keyword-only arguments, you can make your function calls more explicit and easier to understand.
What are advantages and disadvantages of keyword-only arguments?
Advantages and Disadvantages of Keyword-Only Arguments
Advantages
Improved Readability:
By forcing arguments to be named, the code becomes more self-documenting and easier to understand.
Ensures that the function’s API is more explicit, which is particularly useful in public libraries and APIs where clarity is crucial.
Easier Maintenance:
Future changes to the function signature (adding or reordering arguments) are less likely to break existing code, as long as the keyword names remain consistent.
This requires more typing compared to positional arguments.
Learning Curve:
For beginners, understanding and using keyword-only arguments might take some time and effort.
Incompatibility with Positional-Only APIs:
Functions that expect positional-only arguments can be less flexible in some use cases where order is important.
Potential for Overhead:
When overused, the requirement to specify every argument by name can lead to unnecessary overhead in code writing, especially for simple functions.
Example:
def add_numbers(*, a, b):
return a + b
add_numbers(a=1, b=2) # More typing for a simple addition
Summary
Advantages:
Improves code readability and clarity.
Reduces the risk of argument misplacement errors.
Provides flexibility with default values.
Enhances support for public APIs and libraries.
Easier maintenance with future changes to function signatures.
Disadvantages:
Can lead to verbose syntax.
May present a learning curve for new programmers.
Less compatible with positional-only argument expectations.
Potential for overhead in writing and maintaining simple functions.
Keyword-only arguments are a powerful feature in Python that, when used judiciously, can significantly enhance the clarity and robustness of your code.