has been using Python to implement some ideas, recently in the use of Python in the process of such a requirement, defined a function. The first one is the normal number of references. The second is the default, followed by a variable parameter, in the initial learning of Python, you know that the non-keyword variable and keyword variable parameters two, the method of invocation may be various, here is the main proposed a more implicit problem. and the various possible situations are discussed.
function declaration Format
Although Python does not support function overloading, it is largely compensated by support for the many features of function parameters.
The general formula for a function declaration is as follows:
def func(argv1,argv2...[,argv_d = default1,...] [,*var_argv][,**var_key_argv])
This is the general formula for the Python function declaration. The first is a number of common parameters. can also be no, followed by a parameter with a default value. Then the non-keyword variable parameter, and finally the keyword variable parameter. This lays the groundwork for the powerful function calls provided by Python.
function Call
it was in the process of a function call that a problem was encountered . To illustrate. I have defined a function such as the following description:
def f1(a,b=1,*var):print a,‘\t‘,b,‘\t‘,vardef f2(a,b=1,**var):print a,‘\t‘,v,‘\t‘,var
First the normal parameters can be called by location, at the same time using the keyword call. The default value parameters are also used for location matching or keyword calls. The actual proportions are as follows:
The following variable parameters are not used here. Everything is fine.
The problem is. One of the requirements I encountered was that the default number of parameters needed to be called with default values. There are also variable parameters at the same time, and non-keyword, then the following occurs when called:
Can be seen after passing through a non-keyword variable number of parameters. Python defaults to the default number of non-keyword variables (here are 1), because I didn't provide a value for the default number B, which is why I haven't found it in my program.
Then think about it. It is reasonable to find that the Python interpreter makes this decision, since the non-keyword variable arguments are a number of arguments after the default number of parameters. At the time of invocation, there is no value for passing in the default argument. Python cannot infer exactly where it started as a variable parameter, so when a value is not passed to the default argument, it is sequentially extracted from the non-keyword argument (the caller thinks) as the default parameter. This proves from the side that the non-keyword variable parameters are handled in Python with the normal parameter parameters.
Although the above call is somewhat extreme, it is true that this requirement can be replaced with keyword variable parameters, which can be overcome because the interpreter is able to discriminate between keyword and default parameters , such as the following:
Another important issue when using keyword variable parameters is that thekey value of the keyword parameter cannot have a normal parameter or the name of the default parameter is repeated, which is demonstrated from the side Python calls to keyword and keyword variable parameters are stored internally by the same dictionary, and therefore cannot have repeated keys . Calls such as the following:
The above problems encountered in the actual operation of the process encountered, generally from a variety of tutorials, books to learn Python is simply a description of the above function declaration and method of invocation. But almost no such practical operation may encounter the little drops, so learning whatever the best way to do, this truth will never change!
Python variable parameter call function problem