Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use * args and * * kwargs in Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use * args and * * kwargs in Python". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use * args and * * kwargs in Python" can help you solve your doubts.

1. Introduction

In this article, we will discuss * args and * * kwargs in Python and their usage and examples.

Cut the gossip and let's get started.

two。 Problem introduction

When writing a function in Python, we often need to pass values to the function, which are called function parameters.

We might as well take a chestnut as an example. We implement the addition operation as follows:

Def add (xmemy): return x+yprint (add (2p3))

Output:

five

What if, further, we need to achieve the sum of three numbers? Quite simply, we can modify the function to accept three parameters and return the sum of them, as follows:

Def add (x, y, z): return x+y+zprint (add (2,3,5))

Output:

ten

What if we need to sum four numbers at this time? What if it becomes the sum of five numbers again?

Problems arise when we have a variable number of parameters. Should we continue to modify the function to accept the exact number of arguments? Of course not. We won't do that. In fact, the above scenarios are where * args and * * kwargs show their skills.

3. * args in Python

* args allows us to pass a variable number of arguments to the corresponding function in Python. In the function, we should use * before the parameter name to indicate that a variable number of parameters are passed.

The sample code is as follows:

Def add (* args): print (args, type (args)) add (2,3)

The output is as follows:

(2, 3)

Therefore, we determine that these passed parameters create a tuple inside the function, and then we can write the corresponding addition function accordingly, as follows:

Def add (* numbers): total = 0 for num in numbers: total + = num return total

The calling code is as follows:

Print (add (2,3)) print (add (2,3,5)) print (add (2,3,5,7)) print (add (2,3,5,7,9))

The output is as follows:

five

ten

seventeen

twenty-six

It is important to note that the parameter name of the above function is not necessarily args, it can be any other name, in the above code, it is numbers, but it is common practice to use * args as the function parameter name to indicate that the parameter is a variable number of parameters.

4. * * kwargs in Python

Kwargs is an abbreviation for keyword arguments and represents a keyword parameter. * * kwargs allows us to pass a variable number of keyword arguments to the Python function. In the function, we use a double asterisk * * to represent this type of parameter before the parameter name.

The sample code is as follows:

Def total_fruits (* * kwargs): print (kwargs, type (kwargs)) total_fruits (banana=5, mango=7, apple=8)

The output is as follows:

{'banana': 5,' mango': 7, 'apple': 8}

At this point, we can see that in the above case, the dictionary is passed as parameters, and these parameters create a dictionary inside the function, and then we can implement the function total_fruits () to return the total number of fruits.

Def total_fruits (* * fruits): total = 0 for amount in fruits.values (): total + = amount return total

The calling code is as follows:

Print (total_fruits (banana=5, mango=7, apple=8) print (total_fruits (banana=5, mango=7, apple=8, oranges=10)) print (total_fruits (banana=5, mango=7))

The output is as follows:

twenty

thirty

twelve

It is important to note that the name of the parameter is not necessarily kwargs, it can be any name, in the above code, it is * * fruits. However, it is common practice to use * * kwargs as the function parameter name to indicate that the parameter is a variable number of keyword arguments.

After reading this, the article "how to use * args and * * kwargs in Python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report