Python variable parameter call function problem

Source: Internet
Author: User

have been using Python to implement some ideas, recently in the process of using Python, the need to define a function, the first is a normal parameter, the second is a default parameter, followed by a variable parameter, at the time of the initial learning of Python, All know that non-keyword variable parameter and keyword variable parameter two, the way of invocation is also very diverse, here mainly put forward 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 function declarations 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, first of all a number of common parameters, or not, followed by a parameter with a default value, then a 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 like this to illustrate:

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, and can be called by using the parameter keyword. The default value parameter is also used for positional matching or keyword invocation. Examples are as follows:

There is no problem with the following mutable parameters. The problem is that one of the requirements that I encounter is that the default parameter needs to be called with a default value, as well as a mutable parameter, and a non-keyword argument, and the following occurs when called:

As you can see, after passing in a parameter with a non-keyword variable parameter, python defaults to the previous number of non-keyword mutable parameters (here is 1) as the default parameter, because I didn't provide the value of the default parameter B, which is why I never found it in my program. Later self-study, found that the Python interpreter to make such a decision or there is some reason, because the non-keyword variable parameter is a default parameter after a number of parameters, and at the time of the call because there is no value to pass the default parameters, Python can not determine exactly where to start is a mutable parameter, Therefore, when the value of the default parameter is not passed in, the value of the default parameter is extracted sequentially from the non-keyword parameter (caller's opinion), which proves that the non-keyword variable parameter is processed in Python with the default parameter of the normal parameter.
Although the above call is a bit extreme, but it does occur, you can use the keyword variable parameter to replace, the above problem can be solved, because the interpreter can distinguish between the keyword parameters and the default parameters , the call is as follows:

Another important issue when using keyword variable parameters is that the key parameter's key value cannot have a normal parameter or the parameter name of the default parameter is duplicated, which proves from the side that Python is stored internally by the same dictionary for keyword invocation and keyword mutable arguments, and therefore cannot have duplicate keys , the call is as follows:

The above problems encountered in the actual operation of the process encountered, generally from a variety of tutorials, books to learn Python is just a description of the functions described above and call the way, but almost no such practical operation may encounter the point drops, so learn anything the best way to do, this truth will never change!

Python variable parameter call function problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.