Define Python Functions With Unlimited Keyword Arguments Using **kwargs

By Hemanta Sundaray on 2022-02-06

In my blog post here, we learnt how to define Python functions that accept unlimited positional arguments using *args.

In this post,we will learn to define functions that accept unlimited keyword arguments using **kwargs, which is a shorthand for keyword arguments.

def unlimited_keyword_args(**kwargs):
    print(type(kwargs))
    print(kwargs)
    print(kwargs.get('lunch'))


print(unlimited_keyword_args(breakfast="Oatmeal", lunch="Steak"))

# <class 'dict'>
# {'breakfast': 'Oatmeal', 'lunch': 'Steak'}
# Steak

We can see that ** generates a standard dictionary, the values of which we can extract using the values() function.

def unlimited_keyword_args(**kwargs):
    for arg in kwargs.values():
        print(arg)

print(unlimited_keyword_args(breakfast="Oatmeal", lunch="Steak"))

# Oatmeal
# Steak

Join the Newsletter