Python function positional parameters, keyword parameters, variable parameter precedence

Source: Internet
Author: User

def fun (arg,args=1,*arg,**keywords):

Python has a total of these four types of parameters, the first type of the most common, not to say, the second class, keyword parameters, Python can be found by the keyword parameters, the Python function of this feature makes the function parameters more flexible, not necessarily in order to pass, the third class: An asterisk is a non-keyword parameter collection, In the form of a tuple, the fourth class must provide the "pass parameter name = Pass argument value" as the pass parameter.

As for how to preach, I believe we all know that there is not much to say.

To illustrate, these four types of parameters are given precedence in python2, whether they are defining a function or passing a parameter, which is a sequential problem.

Here are the questions that are asked today:

1 defFun (*param,base=3):2result =03      foreachinchparam:4Result + = each5 6Result *=Base7    8     Print('result is', result)9 TenFun (5,1, 2, 3, 4,base=5)

Error:

def fun (*param,base=3):
^
Syntaxerror:invalid syntax

As I said above, this is obviously wrong, that is, the order is wrong. So is it OK to swap the Base,param? Obviously not, as explained above, the same precedence applies when passing parameters. So

Fun (base=5,5,1, 2, 3, 4,5) This is still not possible, because the order is wrong. This is a little awkward, if you want to juggle both, one solution is: Fun (5,5,1, 2, 3, 4,5)
Note: The problem in the above code seems to have been optimized in Python3, and there is no syntax error.

Python function positional parameters, keyword parameters, variable parameter precedence

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.