In [1]:
def func(arg1, arg2, arg3):
print(arg1)
print(arg2)
print(arg3)
In [2]:
d = {'arg1': 'one', 'arg2': 'two', 'arg3': 'three'}
In [3]:
func(**d)
In [4]:
func(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three'})
In [5]:
# func(**{'arg1': 'one', 'arg2': 'two'})
# TypeError: func() missing 1 required positional argument: 'arg3'
In [6]:
# func(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three', 'arg4': 'four'})
# TypeError: func() got an unexpected keyword argument 'arg4'
In [7]:
def func_default(arg1=1, arg2=2, arg3=3):
print(arg1)
print(arg2)
print(arg3)
In [8]:
func_default(**{'arg1': 'one'})
In [9]:
func_default(**{'arg2': 'two', 'arg3': 'three'})
In [10]:
# func_default(**{'arg1': 'one', 'arg4': 'four'})
# TypeError: func_default() got an unexpected keyword argument 'arg4'
In [11]:
def func_kwargs(arg1, **kwargs):
print('arg1', arg1)
for k, v in kwargs.items():
print(k, v)
In [12]:
func_kwargs(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three'})
In [13]:
func_kwargs(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three', 'arg4': 'four'})
In [14]:
func_kwargs(**{'arg1': 'one', 'arg3': 'three'})