Types of parameters for Python:
General parameters
Default parameters
Dynamic parameters
General parameters
Include: Formal parameters and arguments
A formal parameter is a form argument that is simply written in a function and is not assigned and called.
An argument is a parameter that is used when executing a function, and it is assigned in memory
Example:
Test (TXT): Txt,test ()
Output Result: Hello this is a test
Here txt is the formal parameter, ' hello ' is the argument
Default Parameters :
Requirements for Default parameters:
1, does not pass, then uses the default value,
2. The default parameter must be placed at the end of the parameter list
Calculation (x,y,z=): X+y+zcalculation (,) calculation (,,)
Output results
6
7
The default value for Z is 3, and when z is not assigned, x+y+z=1+2+3=6, when assigned to Z 4 o'clock, x+y+z=
dynamic parameter One : A *
Dynamic parameters have no limit on the number of parameters, can accept any parameter
>>> def func (*arg): ... print arg ... >>> func () () >>> func (1) (1,) >>> func (1, 2) >>> func (1, 2, 3)
From the above example, the dynamic parameter can accept 0 or more parameters, each of which is an element in the tuple.
What if the passed parameter itself is a tuple?
>>> a= (11,22,33) >>> func (A) ((11, 22, 33),)
You can see that the tuple itself becomes an element in the tuple, as it is passed into the function as a parameter.
If you do not want this, the result is (11,22,33) This tuple, how to achieve? The answers are as follows:
>>> func (*a) (11, 22, 33)
Add a * before the tuple.
Change a tuple to a list
>>> a[11, 33]>>> func (*a) (11, 22, 33)
Use subscript to get dynamic parameters
>>> def func (*args): ... print args[0]...>>> func (11,22,33) 11
Dynamic parameter two: two *
The function of this dynamic parameter is required for the parameter, and the passed parameter must be
K=value this, get the result is the dictionary
>>> def func (**args): ... func (11,22) ...>>> def func (**args): ... print args...>>> func (1 , Traceback (most recent): File ' <stdin> ', line 1, in <module>typeerror:func () takes exactly 0 a Rguments (2 given) >>> func (k=1) {' K ': 1}>>> func (k=1,k1=22) {' K ': 1, ' K1 ': 22}
Can see the direct 1,22 times wrong. But there's no problem with passing k=1 this form of argument.
You can also pass a dictionary
>>> Dic1 = {' name ': ' Zeng ', ' Age ':28}>>> func (**dic1) {' Age ': ' ' name ': ' Zeng '}>>> func (dic1 ) Traceback (most recent): File "<stdin>", line 1, in <module>typeerror:func () takes exactly 0 Argu ments (1 given)
Note: When passing a dictionary, you also need to add * *, otherwise error
Dynamic parameter three :
The two dynamic parameters above, the first (one *) can only accept parameters of a single value, and the second (two *) can only accept parameters of the form Key=value. The third parameter is the dynamic parameter which can accept the two formal parameters.
>>> def func (*args,**kargs): ... print Kargs ... print args...>>> func (11,22) {} (one, all) >>> ; Func (name= ' Zeng ') {' name ': ' Zeng '} () >>> func (name= ' Zeng ', age=28) {' Age ': +, ' name ': ' Zeng '} () >>> Func (11,22,name= ' Zeng ', age=28) {' Age ': +, ' name ': ' Zeng '} (11, 22)
A typical example of a third dynamic parameter is the Format function
>>> str = ' I am {0},age {1} ' >>> Str.format (' Zeng ', +) ' I am zeng,age 28 '
This notation uses only one of the three dynamic parameters.
>>> str = ' I am {name},age {age} ' >>> Str.format (name= ' Zeng ', age=28) ' I am zeng,age 28
' This notation uses two of the dynamic parameters.
This article is from the "Zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1825873
Introduction to the function parameters of Python