Splat

What is splat

Splat is * or **. And why do we use splat?

Python supports functions with an uncertain number of parameters. The splat operator provides an artistic way of unpacking argument lists.

Positional arguments vs keyword arguments

Positional arguments are arguments without default values while keyword arguments are arguments with default values. Both the number of positional arguments and the number of keyword arguments could be uncertain:

1
def func(*args, **kwargs)

Argument lists

To call the function with uncertain arguments, we could pass the argument one by one:

1
2
3
4
x = 1
y = 2
z = 3
func(x, y, z, kwarg1='key1', kwarg2='key2')

However, we could also pass an argument list and use splat to unpack the list:

1
2
3
test_list = [1, 2, 3]
test_dict = {'kwarg1': 'key1', 'kwarg2': 'key2'}
func(*test_list, **test_dict)