Analysis of parameter definitions and variable parameter usage of functions in Python

Source: Internet
Author: User
This article mainly introduces the parameter definition and variable parameter usage of functions in Python, and analyzes in detail the parameter definition and variable parameter usage methods in Python in the form of examples, for more information, see the following example to describe the parameter definitions and variable parameter usage of functions in Python. Share it with you for your reference. The details are as follows:

When I first learned how to use Python, especially when reading the source code of some libraries, I often see function definitions such as func (* args, ** kwargs, this is a bit confusing. In fact, it is not difficult to understand the definition of function parameters.

Let's talk about the function definition first. we all know that the following code defines a function funcA.

def funcA(): pass

Obviously, the function funcA has no parameters (and does nothing at the same time: D ).

The following function funcB has two parameters,

def funcB(a, b): print a print b

When calling the function, we need to use the function name and the expanded parameter list in parentheses, such as funcB (100, 99). The execution result is:

100
99

Obviously, the order and number of parameters must be consistent with that in the function definition. if funcB (100) is executed, Python will report an error:

TypeError: funcB () takes exactly 2 arguments (1 given)

We can use the default parameter values in the function definition, for example

def funcC(a, b=0): print a print b

In the definition of function funcC, parameter B has a default value and is an optional parameter. if funcC (100) is called, B is automatically assigned a value of 0.

Okay. so far, when we want to define a function, we must pre-define how many parameters (or acceptable) this function requires ). In general, this is okay, but when defining a function, you cannot know the number of parameters (think about the printf function in C). in Python, parameters with * are used to accept variable parameters. Let's look at an example.

def funcD(a, b, *c): print a print b print "length of c is: %d " % len(c) print c

The result of calling funcD (1, 2, 3, 4, 5, 6) is

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

We can see that the first two parameters are accepted by a and B, and the remaining four parameters are accepted by c, where c is a tuple. When we call funcD, we need to pass at least two parameters and more than two parameters in c. If there are only two parameters, c is an empty tuple.

Well, we have figured out a star. it's the next two stars.

In the above example, when a function is called, the transmitted parameters match the parameter table in the function definition according to the position, such as funcB (100, 99) and funcB (99,100) the execution results are different. In Python, you can also call a function using a keyword parameter (keyword argument), that is, when calling a function, specify the parameter value to be paid to that parameter. For example, we call the above funcB (a, B) through these two methods.
FuncB (a = 100, B = 99)
And
FuncB (B = 99, a = 100)

The results are the same as those of funcB (100, 99), because when we call the keyword parameter, we specify to assign 100 to a and 99 to B. That is to say, the keyword parameter can disrupt the order of parameter transmission when calling the function!

In addition, you can use both location-based and keyword-based parameters in function calls. the previous question is to first provide fixed-position parameters, such

def funcE(a, b, c): print a print b print c

The result of calling funcE (100, 99, 98) is the same as that of calling funcE (100, c = 98, B = 99.

Well, after the above preparations, the two stars can finally play:
If the last 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 and passed to the function. for example:

def funcF(a, **b): print a for x in b:  print x + ": " + str(b[x])

Call funcF (100, c = 'hello', B = 200) and run the result

100
C: Hello.
B: 200

As you can see, B is a dict object instance, which accepts the keyword parameters B and c.

Common parameters, * parameters and ** parameters can be used at the same time. how can I use them? Let's take a look at Python Reference Manual, the chapters on Function definitions. Actually, you can guess it out with a dumb, o (∩ _ ∩) o...

I hope this article will help you with Python programming.

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.