Search⌘ K
AI Features

Applying Keyword Arguments

Explore how to apply keyword arguments using Python's partial functions. Understand how to create customized functions like print_csv with partial application, and learn to evaluate simpler alternatives like direct function definitions or lambdas for better code clarity and robustness.

How to apply keyword arguments

A partial function can apply keyword arguments too. Here is a simple example:

Python 3.8
def make_print(sep):
def f(*args):
return print(*args, sep=sep)
return f
print_csv = make_print(', ')
print_colon = make_print(':')
print_csv(1, 2, 3) # 1, 2, 3
print_colon(1, 2, 3) # 1:2:3

The make_print function returns a partial application of the standard print function with the sep keyword argument set to the supplied value. sep is a string that gets inserted between the arguments (this is standard ...