Example analysis of parameter definition and variable parameter usages of functions in Python

Source: Internet
Author: User
This example describes the parameter definitions and variable parameter usages of functions in Python. Share to everyone for your reference. Specific as follows:

Just learn to use Python, especially when looking at some library source code, often see Func (*args, **kwargs) Such a function definition, this * and * * is a bit confusing. In fact, as long as the function parameter definition is clear, it is not difficult to understand.

Let's start with the function definition, as we all know, the following code defines a function Funca

Def Funca (): Pass

Obviously, the function Funca has no parameters (at the same time do nothing: D).

The following function FUNCB has two parameters,

Def FUNCB (A, B): Print a print B

At the time of invocation, we need to use the function name, plus a list of arguments that are expanded in parentheses, such as FUNCB (100, 99), and the result is:

100
99

Obviously, the order and number of parameters are consistent with the function definition, and if you execute FUNCB, Python will error:

TYPEERROR:FUNCB () takes exactly 2 arguments (1 given)

We can use the default values of the parameters in the function definition, such as

Def FUNCC (A, b=0): Print a print B

In the definition of function FUNCC, parameter B has a default value, is an optional parameter, and if we call FUNCC, B automatically assigns a value of 0.

OK, so far, when we define a function, we have to pre-define how many arguments (or how many arguments can be accepted) the function requires. In general, this is fine, but there is also the case when defining a function that does not know the number of arguments (think of the printf function in c), in Python, the parameter with * is used to accept a variable number of parameters. See an example

Def FUNCD (A, B, *c): Print a print b print "Length of C is:%d"% len (c) Print C

Call FUNCD (1, 2, 3, 4, 5, 6) results are

1
2
Length of C is:4
(3, 4, 5, 6)

As we can see, the first two parameters are accepted by a, B, and the remaining 4 parameters are all accepted by C, and C is a tuple here. When we call FUNCD, we have to pass at least 2 parameters, 2 or more parameters, put in C, if there are only two parameters, then C is an empty tuple.

All right, one star, we got it, two stars down.

In the above example, when the function is called, the parameters passed are matched to the parameter table in the function definition according to the position, such as FUNCB (100, 99) and FUNCB (99, 100), the execution result is not the same. In Python, there is also a way to call a function using the keyword argument (keyword argument), which is to explicitly specify the parameter value to pay the argument when calling the function. For example, the above FUNCB (A, b), which we call in either of these ways
FUNCB (a=100, b=99)
And
FUNCB (b=99, a=100)

The result is the same as FUNCB (100, 99), because when we use the keyword parameter invocation, we specify the assignment of the value of 100 to the a,99 to B. In other words, the keyword argument allows us to disrupt the order of the arguments passed when calling the function!

In addition, in a function call, you can mix parameters based on location matching and keyword parameters, the first problem is to give the fixed position of the parameters, such as

Def funce (A, B, c): Print a print b print C

The result of calling Funce (100, 99, 98) and calling Funce (c=98, b=99) is the same.

Well, after the above bedding, two stars can finally come out:
If the last formal parameter in a function definition has a * * (double star) prefix, all other keyword parameters other than the normal parameter will be placed in a dictionary passed to the function, such as:

Def FUNCF (A, **b): Print a For x in B:  print x + ":" + str (b[x])

Call FUNCF (c= ' Hello ', b=200), execute the result

100
C: Hello
b:200

As you can see, B is a Dict object instance that accepts the keyword arguments B and c.

General parameters, * parameters and * * Parameters can be used at the same time, specifically how to use? Look at the Python Reference Manual, the chapters on function definitions. In fact, stupid think can also guess Ah, O (∩_∩) o ...

Hopefully this article will help you with Python programming.

  • Related Article

    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.