Python function parameter type

Source: Internet
Author: User

parameters of the function

To define a function (parameter):

Generic parameters, default parameters, variable parameters (variable position parameters, variable keyword parameters), keyword-only parameters

Call Function (argument):

Position parameter, key word parameter, parameter structure (location parameter deconstruction, keyword parameter deconstruction)


general parameter X,y:

def add (x,y)

return x + y


default parameter x=1:

Def Inc (Base,x =1):

Return base +x

Inc (3)

4

Inc (3,2)

5

A parameter can have a default value, but when a parameter has a default value, the default value is used if the parameter is not passed when invoked

A default value parameter must be followed without a default value parameter, or an error will be syntaxerror

DEF Inc (X=0,y):

Return X+y

File "<ipython-input-19-d6c7b4f89658>", Line 1
    def Inc (x= 0,y):
           ^
syntaxerror:non-default argument Follows default argument
variable position parameters:

When defining a parameter, precede with a *, which means that the parameter is variable and can accept any number of parameters that constitute a tuple and can only be passed by positional parameters


Variable keyword parameters:
When defining parameters, add * * above to indicate that this parameter is variable and can accept any number of arguments that form a dictionary and can only be passed by keyword parameters


Mixed use of parameters:

#位置参数可以和关键字参数一起使用
#当位置可变参数和关键字可变参数一起使用时候, the variable position parameters must be in the front

DEF fn (*args,**kwargs):
    Print (args)
    Print (Kwargs)
FN (1,2,3,x=4,y=5)
(1,2,3)  
(1, 2, 3)
{' Y ': 5, ' X ': 4}

#普通参数可以和可变参数一起使用, but must match when passing the argument

DEF fn (X,y,*args,**kwargs):
    Print (x)
    Print (y)
    Print (args)
    Print (Kwargs)
FN (1,2,3,4,5,a=6,b=7)
1
1
2
(3, 4, 5)
{' A ': 6, ' B ': 7}

#关键字可变参数不允许在普通参数之前

DEF fn (**kwargs,x):

Print (x)

#上面的方式定义会出错


#默认参数可以在可变位置参数之前

DEF fn (X=5,*args):

Print (x)

Pprint (args)


#位置可变参数可以在普通参数之前, but the normal parameter after the position variable parameter becomes a keyword-only parameter


keyword-only Parameters:

Python3 Characteristics

The parameter after #在 * Number can only be passed by keyword parameter called keyword-only parameter

The parameter after the variable position parameter is also the keyword-only parameter


DEF fn (*,x):

Print (x)

FN (1,x=3) #此时x can only be passed by keyword parameters, and if normal parameter passing is installed, it will be eaten by * and variable position parameters

FN (x=3)


DEF fn (*args,x):

Print (args)

Print (x)

#keyword-only parameter can have a default value

The keyword-only parameter can appear with the default parameter, regardless of whether it has a default value, regardless of whether the default parameter is Keyword-only


parameter deconstruction:

An iterative object can be used to form positional parameters

Parameter deconstruction takes place in function calls, and variable parameters occur when the function is defined


Two forms of deconstruction:

An asterisk *, the deconstruction of an object: An iterative object, the result of the deconstruction, the positional parameter

Two asterisks * *, deconstruction objects: dictionaries, deconstruction results, keyword parameters

Keyword argument deconstruction, key must be str type

def add (a,b):
Return a+b
data = [4,3]
Print (Add (*data)) #位置参数解构
Data1 = {' A ': 3, ' B ': 4}
Print (Add (**data1)) #关键字参数解构








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.