def greet(**kwargs):
for _, v in kwargs.items():
print("Hello {}!".format(v))
="Aru", b="Ura") greet(a
Hello Aru!
Hello Ura!
**kwargs
Arumoy Shome
November 1, 2022
Packing and unpacking keyword arguments in Python.
In python, we can unpack keyword arguments using the **kwargs
in the function definition like so.
def greet(**kwargs):
for _, v in kwargs.items():
print("Hello {}!".format(v))
greet(a="Aru", b="Ura")
Hello Aru!
Hello Ura!
Turns out, that we can convert the unpacked dict into back into keyword arguments and pass it along to another function as well! This is done like so.
def greet(**kwargs):
for _, v in kwargs.items():
print("Hello {}!".format(v))
meet(**kwargs)
def meet(a=None, b=None):
print("Nice to meet you again, {} & {}".format(a, b))
greet(a="Aru", b="Ura")
Hello Aru!
Hello Ura!
Nice to meet you again, Aru & Ura