Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 19:30:38 10月26日 Update

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!

Tags: Parameters functions asterisks two code examples results output learning content form double stars number at the same time dictionary that is ideas situations articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB Docker Redmi Microsoft