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

What is the difference between the first parameter and two asterisks in the Python function?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the difference between the first one and two asterisks in the Python function". The content in the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "what is the difference between the first one and two asterisks in the Python function".

It is common to see one or two asterisks preceded by input parameters in Python functions, for example:

Def foo (param1, * param2): def bar (param1, * * param2):

Both of these uses are actually used to import any number of parameters into the Python function.

Single star (*): * agrs

Import all parameters as tuples (tuple):

Example

Def foo (param1, * param2): print (param1) print (param2) foo

The output of the above code is:

1 (2, 3, 4, 5)

Double star (* *): * * kwargs

Double asterisks (*) import parameters in the form of dictionaries:

Example

Def bar (param1, * * param2): print (param1) print (param2) bar

The output of the above code is:

1 {'await: 2,' baked: 3}

In addition, another use of a single asterisk is to extract the parameter list:

Example

Def foo (runoob_1, runoob_2): print (runoob_1, runoob_2) l = [1,2] foo (* l)

The output of the above code is:

1 2

Of course, these two uses can appear in the same function at the same time:

Example

Def foo (a, baked 10, * args, * * kwargs): print (a) print (b) print (args) print (kwargs) foo (1, 2, 3, 4, eBay 5, fudge 6, Gmail 7)

The output of the above code is:

12 (3,4) {'eyed: 5,' faded: 6, 'gathers: 7} Thank you for reading, the above is the content of "what is the difference between the first one and two asterisks in the Python function?" after the study of this article, I believe you have a deeper understanding of the difference between the first one and two asterisks in the Python function, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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