The difference between function parameters *args and **kw in Python

Source: Internet
Author: User

1. Functions and parameters (actual parameters)

The creation of a function in Python is Def, the function name is Def f (), the F function name, Def f (A, b), here A-B is two parameters, the function name is custom, and the parameters are custom, so be free. Look at the following effect:

  

Here f is the argument, and then the function above is called.

The following explains the difference between *args and **kw. 2.*args

  

The value behind the * is custom, as long as it is not a number, it can be defined as *ABC,*CCC, and Len () is the function, which means to return the length of the string. Then the front of the B is the normal parameter, print a print B is echo 1, 2, because I call F (1,2,3,4,5), remove two ordinary parameters, the remaining three values are 4,5,6, as the name implies the length of 3.

3.**kw

  

* * and * custom are the same, * * is also custom, followed by a number on the line. What I'm defining here is **d, here is not the same, here you will find a loop here, and then in print, you may not understand, you look at the bottom of the F () function call, call the final answer to a key-value pair form, and then look at the above print x+ ': ' +str (C[x]) will be relaxed, you will say what STR is, str () is a function, his meaning is that the number of variables or constants can be changed to a character variable or constant. So when we call him, name= ' Zhangsan ' is quoted here because this is a string, and age=13 is not quoted, because 13 is an integer type.

4.* and * * Combined usage

  

Because I have spoken in front of the use of * and * *, merged together, they are unchanged, the position has not changed. When you see this here you will say * and * * What is the difference, yes, their difference is that * contains other numbers, in this case *c is a tuple, and **d here contains a string or according to print x+ ': ' +str (D[x]), the type is XX=XX, Contained in D, so that we can clearly distinguish and learn it!

The difference between function parameters *args and **kw in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.