The distinction of several different types of parameters in Python and the order in which they are defined

Source: Internet
Author: User
Tags function definition

In Python, the parameters of a function include, in addition to the most basic, required parameters, the default parameters, mainly the following special types of parameters: variable parameters, keyword parameters, named keyword parameters. Their existence makes the parameters of the function more free. When functions are defined, they are defined in the order that they are required, default, variable, named keyword, and keyword parameters. Variable Parameters

The number of parameters passed in by the variable parameter is variable.

When defining a variable parameter and defining a list or tuple parameter, only a * number is added to the parameter. Inside the function, the parameter numbers receives a tuple, so the function code is completely unchanged. However, when you call the function, you can pass in any of the arguments, including 0 parameters:

Def calc (*numbers):

sum = 0

For n innumbers:

Sum =sum + N * N

return sum

>>>calc (1, 2)

5

>>>calc ()

0

>>>nums= [1, 2, 3]

>>>calc (*nums)

keyword parameter

Variable parameters allow you to pass in 0 or any of the parameters, which are automatically assembled as a tuple when the function is called. The keyword parameter allows you to pass in 0 or any parameter with a parameter name, which is automatically assembled into a dict within the function. Take a look at the example:

Defperson (name, age, **kw):

Print (' name: ', Name, ' Age: ', age, ' other: ', kw)

>>>person (' Adam ', gender= ' M ', job= ' Engineer ')

Name:adam age:45 Other: {' Gender ': ' M ', ' job ': ' Engineer '}

>>>extra= {' City ': ' Beijing ', ' job ': ' Engineer '}

>>>person (' Jack ', **extra)

Name:jack age:24: {' City ': ' Beijing ', ' job ': ' Engineer '}

**extra said that extra this dict all key-value with keyword parameters passed to the function of the **kw parameter, KW will get a dict, note kw dict is a copy of the extra, the changes to KW will not affect the extra outside the function. named keyword parameter

If you want to restrict the name of a keyword parameter, you can use a named keyword parameter, for example, to receive only city and job as key parameters. The functions defined in this way are as follows:

def person (Name, age,*, City, Job):

Print (Name,age, city, Job)

If a variable parameter is already in the function definition, a special delimiter is no longer required for the named keyword argument followed:

The named keyword parameter must pass in the parameter name, which is different from the positional parameter. If the parameter name is not passed in, the call will error:

you define functions in Python by using required parameters, default parameters, variable parameters, keyword parameters, and named keyword parameters, all of which can be combined in a combination of 5 parameters. Note, however, that the order of the parameter definitions must be: required, default, variable, named keyword, and keyword parameters.

deff1 (A, B, c=0, *args, **kw):

Print (' A = ', A, ' B = ', B, ' C = ', C, ' args = ', args, ' kw = ', kw)

deff2 (A, B, c=0, *, D, **kw):

Print (' A = ', A, ' B = ', B, ' C = ', C, ' d = ', D, ' kw = ', kw)

>>>args= (1, 2, 3, 4)

>>>kw= {' d ': ', ' x ': ' # '}

>>>F1 (*ARGS,**KW)

A = 1 b = 2 c = 3 args = (4,) kw = {' d ': $, ' x ': ' # '}

Therefore, for any function, it can be invoked in the form of a Func (*ARGS,**KW), regardless of how its arguments are defined.

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.