Pass kwargs to another function in Python


Python lists two types of arguments that can be passed to functions in a program. Non-keyword arguments ( **args) and keyword arguments ( **kwargs).

In general, python functions must be called with the correct number of arguments. If the function requires two parameters, we should only pass two.

In this article, we will discuss how to use keyword arguments and how to pass keyword arguments to another function.


Keyword arguments in Python

Often, parameters do pass information to a function. Keyword arguments are a method that can be used to pass arguments to functions.

Specifically with keyword arguments, we can send arguments using the key=value syntax. Let's see an example below.

def keyword_function(fname,lname,age):
    print("first name is " + fname )

keyword_function(fname= "Anne",lname= "Steaven" ,age= 27)

This example defines a keyword_functionfunction named . It passes three parameters named fname, lname and age.

Therefore, these arguments are sent as keyword arguments, because each argument takes a key-value pair when the function is called in the program.

output:

In the above output, we printed a statement passing the fname parameter.


Calling functions with **kwargs in Python

Keyword arguments can mainly be used when several arguments are unknown. In this case, add two asterisks ( ) before the parameter name in the function definition **.

If the function has a **kwargsparameter, it accepts a variable number of keyword arguments as a dictionary.

Consider the following example.

def animals(**kwargs):
    print(kwargs)

animals(name1='Rabbit',name2='Dog',name3='Monkey')

In the code above there is a function called animals. It has one **kwargsparameter named and the function call takes three arguments as keyword arguments.

output:

Calling functions with **kwargs in Python

We can think of the result as a dictionary with two pairs of value and key. The kwargs parameter is by convention.

So as parameter name we can use any meaningful name. Again, this is how they call **kwargsfunctions with parameters.


Pass kwargs to another function using Python

code:

def info(**):
    x = dict(name="shen",age=27,degree="SE")
    pass_func(**x)

def pass_func(**kwargs):
    for i in kwargs:
        print(i,kwargs[i])
info()

Using the code above, we print information about the person such as name, age, and degree. The program passes kwargs to another function that contains the variable x that declares the dict method.

Example definition function information without any arguments. The function info declares a variable x, which defines three key-value pairs. Usually, key-value pairs are defined as dictionaries in Python.

Therefore, the dict method specifies a pair of key and value. Since the kwargs unpack arguments are declared as a dictionary type, we can use the dict method in this example.

Then we have another function called pass_func which passes the kwargs argument. This function includes a for loop that prints the kwargs parameter, after that, we can pass the kwargs function to the info function to print x.

Therefore, we call pass_func with the kwargs argument of the x variable. Finally, we call the info function in the pass_func function.

output:

Pass kwargs to another function using Python


Summarize

Throughout this article, we focus on how to pass kwargs to another function. The kwargs parameter can get multiple variables, each with undefined parameters.

When passing a kwargs argument to a function, it must use double asterisks and the argument name **kwargs. When passing kwargs to another function, first create a parameter with two asterisks, then we can pass that function to another function as our purpose.

Guess you like

Origin blog.csdn.net/fengqianlang/article/details/132136661