I. Types of function parameters
The kind of function parameter definition and transfer method we contacted before is called positional parameter, that is, the parameter is matched by position, from left to right, and then in order, this has strict requirement to the position and number of parameters. And in Python there is a parameter name to match, so that you do not need to strictly follow the parameters defined by the position to pass the parameter, this parameter is called the keyword parameter .
>>>defdisplay (A, b):PrintaPrintb
>>> Display ('Hello',' World')#positional parameters, where parameters are matched by locationHelloWorld>>> Display ('Hello')#This will be an error .Traceback (most recent): File"<pyshell#30>", Line 1,inch<module>Display ('Hello') Typeerror:display () takes exactly2 Arguments (1given)
>>> Display (' World','Hello') Worldhello>>> >>> Display (a='Hello', b=' World')#keyword ParametersHelloWorld>>> Display (b=' World', a='Hello') HelloWorld#you can see that the parameter position has no effect on the result when the parameter is passed by specifying the parameter name.
The most powerful thing about keyword parameters is that it provides default values for function parameters
>>>defDisplay (a='Hello', b='wolrd'):#default parameters are specified for a and B, respectively PrintA +b
>>> display ()hellowolrd>>> Display (b=' World' # If you don't pass arguments to a or B, they take the default value HelloWorld>>> Display (a='Hello') hellowolrd>>> Display (' World')#does not specify whether ' world ' is passed to a or B, the default is a left-to-right match, which is passed to aWorldwolrd
Note: When a function is called repeatedly, the default shape participant inherits the value of the parameter after the call ends
>>>defInsert (a,l=[]): L.append (a)PrintL
>>> Insert ('Hello')['Hello']>>> Insert (' World') ['Hello',' World']#when a function is called repeatedly, the default shape participant inherits the value of the parameter at the end of the previous call
Two, any number of parameters
In the inability to determine the number of parameters, you can use the collect parameters, using the collection parameters simply precede the parameters with ' * ' or ' * * '.
' * ' and ' * * ' indicate the ability to accept 0 to any number of arguments, '*' means that no matching values are placed in the same tuple , '* *' means that no matching values are placed in a dictionary .
>>>defMul (*arg):#A parameter that starts with a *, representing a tuple PrintArg
>>> Mul (A'Ethon')(1, 2, 3,'Ethon')>>> >>>defMUL2 (**arg):#A parameter that starts with a ** , which represents a dictionary PrintArg
>>> Mul2 (a=1,b=2,c=3,d='Ethon') {'a': 1,'C': 3,'b': 2,'D':'Ethon'}
Python basic 11 (function three)