function parameters in the actual application process is also relatively broad, such as in the registration, some information to fill, some of the parameter types are required parameters, some variable parameters, some default parameters, and so on, these are through the function of the parameter decision, with the user input data as parameters passed, call the entire function to complete function. We mainly share with you today, in Python some of the common parameters, such as positional parameters, default parameters, keyword parameters, variable parameters, combined parameters and other practical application examples to facilitate the function of the parameters have a further understanding and learning.
First, Position parameters
The simple point of understanding is the relationship with the position.
def Say_hello (name,age): print"hello,%s my age is %s"%(name, Age) Say_hello ('zhangshan', 134)
When calling Say_hello (' Zhangshan ', 134) function, ' Zhansgahn ' is in front, 134 is behind. So the print out result is
is 134
But what if you change the actual location of the argument?
def Say_hello (name,age): print"hello,%s my age is %s"%(name, Age) Say_hello (134,'zhangshan')
Put 134 in front of the argument, ' Zhangshan ' in the back of Say_hello (), so what is the result of the call to print? Let's look at the results:
is ' Zhanshan '
This order is not a bit of a problem, this is based on the location of the argument to the parameters of the decision, the argument of the 134 given the name, the actual argument ' Zhanshan ' to the age, thereby outputting such an effect.
We continue to talk about the second way of communicating the parameters:
Default parameters: You can interpret the default parameters as a procedure for assigning a value to a normal parameter when defining a function. But this assignment is not an arbitrarily assigned value, but also to have a routine, what is the routine? When we assign the value, we must put the default parameters after the normal parameters, remember that the default parameters must not be placed in front of the normal parameters, once put in front, the execution of the entire function will be error, we can try. OK, you know this, then let's create a function with the default worth to see the effect:
1) Default parameters are placed after normal parameters
def Add (a,b=40): return A +b Add (60)
Code parsing:
1. Define the Add function, assign a value to B equals 40,b=40 is the default parameter of the function, A is normal, satisfy the default parameters before the normal parameters
2. The calling function passes an actual value of 60 to the Add () function, and the 60 argument is passed to a, which is the a=60,b using the default value
3. So the whole function returns the result is a+b=60+40 = 100
4. If we pass the argument to AD (50,50) Then there will be a big change in the result (this can be demonstrated by yourself)
For the return result of the 4th to do some explanation, when the function has a default value, if the argument is passed to the parameter, then the function will take precedence on the value of the argument to use, the default value will be overridden directly. This is the second feature of the default parameter. We are looking at the third point of knowledge for the default parameter. Default parameters can not be placed in front of the normal parameters, if you put, then what is the result?
2) Default parameters are placed in front of normal parameters
def Add (b=40, a): return A +b Add (60)
We b=40 the default parameters in front of the normal parameters of a. Then on this basis, we run the code, will find the program error, error information as follows.
Syntaxerror:non-default argument follows default argument probably means grammatical errors: SyntaxError: Non-default parameters follow the default parameters, telling you to put the normal parameters behind the default parameters.
The computer's error message is understandable, because his processing logic is:
1.60 to B,a forgot to pass the argument
2.60 for A, B with the default value of 40 for calculation
That means 2 results will happen, but the computer doesn't know what our real intentions are. The program runs with only one result and does not appear to be 2. Just like you look for a wife, must be single-minded, can not change every day, every day for the last must be a matter of.
OK, let's go on to the third parameter demo:
Three, keyword parameters
The simple understanding is that the formal parameter equals the argument and ignores the parameter position. Let's look at an example.
def Add (b): return +b Add (a=60,b=20)
Still the same example, call the Add () function, call the function to specify A=60,B=20, at run time function processing is direct to tell the parameter a=60,b=20 directly to function inside, and this and position have no relation. In other words, you can change the position of b=20,a=60 so that the printing results are the same, and the previous operation without any changes. Of course this is the first user of the keyword parameter, we look at the second kind.
We define a function to implement a group of students ' information, then this function name we define as Strudentinfo
defStudentinfo (name,age,address='Beijing'): Print 'name', namePrint ' Age', AgePrint 'Address', Addressstudentinfo ('Zhanshan', 150) Studentinfo ('Zhanshan', 150,address='Nanchang')
Operation Result:
Name Zhanshanage 150address beijingname zhanshanage 150address Nanchang
It is not difficult for us to find the result of the first call and the result of the second call the value of address has changed. Because we assign the address to Nanchang for the second time, of course, the address is not re-assigned value, directly to the Nanchang this parameter to the third position, the effect is the same, the function will take precedence of the use of arguments.
defStudentinfo (name,age,address='Beijing'): Print 'name', namePrint ' Age', AgePrint 'Address', Addressstudentinfo ('Zhanshan', 150) Studentinfo ('Zhanshan', 150,'Nanchang')
Results of the output:
Name Zhanshanage 150address beijingname zhanshanage 150address Nanchang
Let's continue to change the example above:
defStudentinfo (name,age,address='Beijing'): Print 'name', namePrint ' Age', AgePrint 'Address', Addressstudentinfo ('Zhanshan', age=150,address='Nanchang')
The operating result is:
Address Nanchang
What if we want to define a keyword parameter that can receive multiple parameters?
defStudentinfo (name,age=150,**kw):Print 'name is', namePrint ' Age is', AgePrint ' City is', KWPrint 'Address is', Kwstudentinfo ('Zhangshan', 15,city='Beijing', address='Nanchang')
**KW with the table is can receive multiple keyword parameters, is the keyword parameter, not the default parameter oh. The result of the output is:
Name isZhangshanage is15 City is{' City':'Beijing','Address':'Nanchang'}address is{' City':'Beijing','Address':'Nanchang'}
Because the parameters received are not fixed, all the received parameters are stored uniformly in the dictionary upon return. We can also store the keyword parameters as a dictionary to invoke, see the correct posture.
defStudentinfo (name,age=150,**kw):Print 'name is', namePrint ' Age is', AgePrint ' City is', KWPrint 'Address is', Kwdict={' City':'Beijing','Address':'Shenzhen','Home':' China'}studentinfo ('Zhangshan', 1500,**dict)
So the result of the output is as follows:
Name isZhangshanage is1500 City is{'Home':' China','Address':'Shenzhen',' City':'Beijing'}address is{'Home':' China','Address':'Shenzhen',' City':'Beijing'}
The results returned by the keyword parameter are also stored in a dictionary.
Four, variable parameters
Getting Started with Python basics-function parameters