parameter definition of functions in Python and examples of variable parameter usages

Source: Internet
Author: User
Tags definition function definition in python


The examples in this article describe the parameter definitions and variable parameter usages of functions in Python. Share to everyone for your reference. Specifically as follows:



When learning 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 definition of function parameters are clear, it is not difficult to understand.



To start with the function definition, we all know that the following code defines a function Funca




Def Funca (): Pass


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



The following function, FUNCB, has two parameters.




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


Call, we need to use the function name, plus the parentheses to expand the list of parameters, such as FUNCB (100, 99), the results are:



100



99



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



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



We can use parameter defaults 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, an optional argument, and if we call FUNCC, B is automatically assigned a value of 0.



OK, so far, when we're defining a function, we have to define in advance how many parameters (or how many parameters can be accepted) for this function. Generally this is OK, but there are also in the definition of the function, you can not know the number of parameters (think of the C language in the printf function), in Python, with the parameters of the * is used to accept the variable number of parameters. Look at 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) The result is



1


2



Length of C is:4



(3, 4, 5, 6)



We see that the first two parameters were accepted by a, B, the remaining 4 parameters, all received by C, C here is a tuple. When we call FUNCD, we must pass at least 2 parameters, 2 or more parameters, are put into C, if there are only two parameters, then C is a empty tuple.



All right, one star. We made it clear that there are two stars in the next round.



In the example above, when calling a function, the parameters passed are matched by the position to the parameter table in the function definition, such as FUNCB (100, 99) and FUNCB (99, 100). In Python, a method of calling a function with a keyword parameter (keyword argument) is also supported, that is, when calling a function, explicitly specifying the parameter value to pay that formal parameter. For example, or the top FUNCB (A, b), which we call in both ways



FUNCB (a=100, b=99)



And



FUNCB (b=99, a=100)



The result is the same as FUNCB (100, 99), because when we call with the keyword parameter, we specify a value of 100 assigned to B to a,99. That is, keyword parameters allow us to disrupt the order in which the parameters are passed when the function is invoked!



In addition, in function calls, you can mix parameters and keyword parameters based on positional matching, which first gives the parameters of a fixed position, 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 matting, 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 parameters, are 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 (c= ' Hello ', b=200), execution results



100



C: Hello



b:200



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



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



I hope this article will help you with your 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.