Class KeywordArguments
**kwargs.
When to Use
KeywordArguments must be used whenever a Python
function accepts named arguments (both required and optional). This ensures
proper mapping of Java arguments to Python function parameters, especially
when working with Python methods that utilize **kwargs.
Important: An instance of KeywordArguments must always be the
last argument when invoking a Python function. It may be preceded by an
instance of PositionalArguments, but KeywordArguments must
always be the final argument. This ensures proper alignment with Python's
argument structure.
The KeywordArguments class provides factory methods to create
instances from a Map or by directly specifying key-value pairs.
Usage
Consider the following Python function:
def example_function(*, named1, named2, named3=None, named4=42):
...
In this function, named1 and named2 are required
keyword arguments, while named3 and named4 are
optional because they have default values.
From Java, this function can be invoked as:
value.invokeMember("example_function", kwArgs);
The variable kwArgs can be created in several ways:
Using a map:
KeywordArguments kwargs = KeywordArguments.from(Map.of("named1", 10, "named2", "value", "named4", 100));
Using key-value pairs:
KeywordArguments kwargs = KeywordArguments.of("named1", 10, "named2", "value", "named4", 100);
Using a builder:
public static final class ExampleFunctionKwArgsBuilder {
private final Map<String, Object> values = new HashMap<>();
// Constructor includes all required arguments
public ExampleFunctionKwArgsBuilder(Object named1, Object named2) {
values.put("named1", named1);
values.put("named2", named2);
}
// Methods for optional arguments
public ExampleFunctionKwArgsBuilder named3(Object named3) {
values.put("named3", named3);
return this;
}
public ExampleFunctionKwArgsBuilder named4(Object named4) {
values.put("named4", named4);
return this;
}
// Add dynamic arguments
public ExampleFunctionKwArgsBuilder add(String key, Object value) {
values.put(key, value);
return this;
}
// Build the KeywordArguments instance
public KeywordArguments build() {
return KeywordArguments.from(values);
}
}
Using the builder to create the arguments:
KeywordArguments kwargs = new ExampleFunctionKwArgsBuilder("value1", "value2") // Required keys
.named4(100) // Optional key
.add("dynamicKey", 42) // Dynamic key
.build();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic KeywordArgumentsCreates aKeywordArgumentsinstance from the specified map.static KeywordArgumentsCreates aKeywordArgumentsinstance from a single key-value pair.static KeywordArgumentsCreates aKeywordArgumentsinstance from multiple key-value pairs.static KeywordArgumentsCreates aKeywordArgumentsinstance from multiple key-value pairs.static KeywordArgumentsof(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) Creates aKeywordArgumentsinstance from multiple key-value pairs.static KeywordArgumentsof(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5) Creates aKeywordArgumentsinstance from multiple key-value pairs.static KeywordArgumentsof(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6) Creates aKeywordArgumentsinstance from multiple key-value pairs.static KeywordArgumentsof(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6, String name7, Object value7) Creates aKeywordArgumentsinstance from multiple key-value pairs.static KeywordArgumentsof(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6, String name7, Object value7, String name8, Object value8) Creates aKeywordArgumentsinstance from multiple key-value pairs.static KeywordArgumentsof(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6, String name7, Object value7, String name8, Object value8, String name9, Object value9) Creates aKeywordArgumentsinstance from multiple key-value pairs.static KeywordArgumentsof(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6, String name7, Object value7, String name8, Object value8, String name9, Object value9, String name10, Object value10) Creates aKeywordArgumentsinstance from multiple key-value pairs.
-
Method Details
-
from
Creates aKeywordArgumentsinstance from the specified map.- Parameters:
values- a map containing the keyword arguments; each key-value pair represents a named argument. Keys must be non-null strings, and values can be any object.- Returns:
- a new
KeywordArgumentsinstance containing the provided arguments - See Also:
-
of
Creates aKeywordArgumentsinstance from a single key-value pair.- Parameters:
name- the name of the argument; must not benull.value- the value of the argument; must not benull.- Returns:
- a new
KeywordArgumentsinstance containing the specified argument - See Also:
-
of
Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-
of
public static KeywordArguments of(String name1, Object value1, String name2, Object value2, String name3, Object value3) Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argumentname3- the name of the third argumentvalue3- the value of the third argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-
of
public static KeywordArguments of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argumentname3- the name of the third argumentvalue3- the value of the third argumentname4- the name of the fourth argumentvalue4- the value of the fourth argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-
of
public static KeywordArguments of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5) Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argumentname3- the name of the third argumentvalue3- the value of the third argumentname4- the name of the fourth argumentvalue4- the value of the fourth argumentname5- the name of the fifth argumentvalue5- the value of the fifth argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-
of
public static KeywordArguments of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6) Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argumentname3- the name of the third argumentvalue3- the value of the third argumentname4- the name of the fourth argumentvalue4- the value of the fourth argumentname5- the name of the fifth argumentvalue5- the value of the fifth argumentname6- the name of the sixth argumentvalue6- the value of the sixth argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-
of
public static KeywordArguments of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6, String name7, Object value7) Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argumentname3- the name of the third argumentvalue3- the value of the third argumentname4- the name of the fourth argumentvalue4- the value of the fourth argumentname5- the name of the fifth argumentvalue5- the value of the fifth argumentname6- the name of the sixth argumentvalue6- the value of the sixth argumentname7- the name of the seventh argumentvalue7- the value of the seventh argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-
of
public static KeywordArguments of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6, String name7, Object value7, String name8, Object value8) Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argumentname3- the name of the third argumentvalue3- the value of the third argumentname4- the name of the fourth argumentvalue4- the value of the fourth argumentname5- the name of the fifth argumentvalue5- the value of the fifth argumentname6- the name of the sixth argumentvalue6- the value of the sixth argumentname7- the name of the seventh argumentvalue7- the value of the seventh argumentname8- the name of the eighth argumentvalue8- the value of the eighth argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-
of
public static KeywordArguments of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6, String name7, Object value7, String name8, Object value8, String name9, Object value9) Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argumentname3- the name of the third argumentvalue3- the value of the third argumentname4- the name of the fourth argumentvalue4- the value of the fourth argumentname5- the name of the fifth argumentvalue5- the value of the fifth argumentname6- the name of the sixth argumentvalue6- the value of the sixth argumentname7- the name of the seventh argumentvalue7- the value of the seventh argumentname8- the name of the eighth argumentvalue8- the value of the eighth argumentname9- the name of the ninth argumentvalue9- the value of the ninth argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-
of
public static KeywordArguments of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4, String name5, Object value5, String name6, Object value6, String name7, Object value7, String name8, Object value8, String name9, Object value9, String name10, Object value10) Creates aKeywordArgumentsinstance from multiple key-value pairs.- Parameters:
name1- the name of the first argumentvalue1- the value of the first argumentname2- the name of the second argumentvalue2- the value of the second argumentname3- the name of the third argumentvalue3- the value of the third argumentname4- the name of the fourth argumentvalue4- the value of the fourth argumentname5- the name of the fifth argumentvalue5- the value of the fifth argumentname6- the name of the sixth argumentvalue6- the value of the sixth argumentname7- the name of the seventh argumentvalue7- the value of the seventh argumentname8- the name of the eighth argumentvalue8- the value of the eighth argumentname9- the name of the ninth argumentvalue9- the value of the ninth argumentname10- the name of the tenth argumentvalue10- the value of the tenth argument- Returns:
- a new
KeywordArgumentsinstance containing the specified arguments - See Also:
-