The description of of parameter with asterisk for passing of function parameter in python

  • 2020-04-02 09:29:49
  • OfStack

Two other aspects of the use of function parameters are worth noting:
1. How are function parameters defined
2. How are arguments parsed during function calls

First, there are four main ways to define a function parameter in python:
1. F (arg1, arg2,...).
This is the most common way, the definition of a function can define any parameters, comma separated between each parameter, the functions defined in this way at the time of call must also provide the number in parentheses after the function name is equal to the value of the (real) parameters, and the order must be the same, that is to say, in this kind of call way, parameter and the number of arguments must be consistent, and must be one to one correspondence, that is to say, the first parameter corresponding to the first argument. Cases such as:
Def a (x, y) :
Print the x, y
Call this function, a(1,2), then x is 1, y is 2, corresponding to the form participation argument, if a(1) or a(1,2,3) will report an error.


2. F (arg1, arg2 = value2,...).
This is an improved version of the first, which provides default values
Def a (x, y = 3) :
Print the x, y
If I call this function, a(1,2) is still x equals 1 and y equals 2, but if I call a(1), I'm not going to get an error, so x is still 1 and y is the default 3. In the above two ways, we can also change the parameter position, for example, a(y=8,x=3) is also acceptable in this form.


3. (F * arg1)
The top two methods are to pass in as many arguments as there are parameters, but sometimes it is not certain how many parameters, then the third method is more useful, it is a * with the parameter name to represent the function of the number of arguments is variable, may be 0 or may be n. Notice that no matter how many there are, all of them are stored inside the function in a tuple with a parameter named identifier.
> > > Def a (* x) :
If len (x) = = 0:
Print 'None'
The else:
Print x
> > > A (1)
(1,) # is stored in a tuple
> > > (a)
None
> > > A (1, 2, 3)
(1, 2, 3)
> > > A (m = 1, y = 2, z = 3)

Traceback (most recent call last):
The File" < Pyshell# 16 > "Line 1, in-toplevel -
A (m = 1, y = 2, z = 3)
TypeError: a() got an unexpected keyword argument 'm'


4. F (arg1) * *
Two * before the parameter name indicates that the argument will be stored inside the function in a dictionary of the form named identifier, and the method to call the function needs to be in the form of arg1=value1 and arg2=value2.
> > > Def a (* * x) :
If len (x) = = 0:
Print 'None'
The else:
Print x
> > > (a)
None
> > > A (x = 1, y = 2)
{'y': 2, 'x': 1} # is stored in the dictionary
> > > The call to a(1,2) # reports an error

Traceback (most recent call last):
The File" < Pyshell# 25 > "Line 1, in-toplevel -
A (1, 2)
TypeError: a() takes exactly 0 arguments (2 given)


It defines four ways above, then the function parameters in the process of call what is parsed, the above four methods in fact as long as remember priority in order to reduce, 1 first, and after 2, 3, the last four, namely the arg in mode 1 first, and then arg in analytic way 2 = value, analytic way 3 again, is the extra arg this form of argument of a tuple handed in, finally the remaining key = value of this kind of argument in the form of a dictionary to bring two asterisks parameter, is 4.
> > > Def test (a, x, y = 1, * * * b) :
Print the x, y, a, b


> > > Test (1)
1 (1) {}
> > > Test (1, 2)
2 (1) {}
> > > Test (1, 2, 3)
2 (3, 1) {}
> > > Test (1, 2, 3, 4)
1, 2 (3, 4) {}
> > > The test (x = 1, y = 2)
2 (1) {}
> > > Test (1, a = 2)
1 1 () {'a': 2}
> > > Test (1, 2, 3, a = 4)
1, 2 (3,) {'a': 4}
> > > Test (1, 2, 3, y = 4)

Traceback (most recent call last):
The File" < Pyshell# 52 > "Line 1, in-toplevel -
Test (1, 2, 3, y = 4)
TypeError: test() got multiple values for keyword argument 'y'

Related articles: