What is the **params syntax in a Python method definition?

What is the **params syntax in a Python method definition?

Consider a function with default arguments:

def func(foo=3):
    print(foo)

The structure of the arguments is (in principle) very similar to a dictionary. The function foo has (essentially) a dictionary of default arguments (in this case {foo:3}). Now, lets say that you dont want to use the keyword in the function call, but you want to use a dictionary instead — then you can call foo as:

d = {foo:8}
func(**d)

This allows you to dynamically change what arguments you are passing to the function func.

This become a little more interesting if you try the following:

d = {foo:8, bar:12}
func(**d)

This doesnt work (it is equivalent to foo(foo=8, bar=12), but since bar isnt a valid argument, it fails).

You can get around that problem by giving those extra arguments a place to go inside the definition of foo.

def func( foo=3, **kwargs ):
    print(foo,kwargs)

Now, try:

d = {foo:8, bar:12}
func(**d)  #prints (8, {bar:12})

All the extra keyword arguments go into the kwargs dictionary inside the function.

This can also be called as:

func(foo=8, bar=12)

with the same result.

This is often useful if funcA calls funcB and you want funcA to accept all of the keywords of funcB (plus a few extra) which is a very common thing when dealing with classes and inheritance:

def funcA(newkey=None,**kwargs): 
    funcB(**kwargs)

Finally, here is a link to the documentation

The **params parameter represents all the keyword arguments passed to the function as a dictionary.

What is the **params syntax in a Python method definition?

Leave a Reply

Your email address will not be published. Required fields are marked *