Parameters of the python3.x function

Source: Internet
Author: User

2016-08-09 15:06:18

Position parameters

When a function is called, the arguments are passed to the function by position

1 def Show (A1,A2,A3,A4): 2     Print (A1,A2,A3,A4) 3 4 Show ('s','h','o  ','w')

The results of the implementation are as follows:

S H o W
Default parameters

Set the default values for the last few parameters of the function, and if the arguments are not passed, the function assigns a value to the parameter using the default value.

The default parameter must be the last or several of the function's formal parameters, and if not, Python will prompt for a syntax error.

  

1 def Show (a1,a2,a3='C', a4='a'):2      Print(a1,a2,a3,a4)34 Show ('s' ,'h')

The results of the implementation are as follows:

S h c A
Specifying parameters

When calling a function, you can specify which parameter to assign to. When you use the specified parameters, you can write in a parameter order. You can skip one or several default parameters in the middle.

1 def Show (a1,a2,a3='C', a4='a'):2      Print(a1,a2,a3,a4)34 Show ('s' , a4='g', a2='d')

The results of the implementation are as follows:

S d C G
Variable parameters

function definition, you can add an * number before the parameter. If you add the * number, you can pass multiple arguments like a function, and they will be handled internally as a tuple.

When you call this function, you can pass any parameter, including 0 parameters.

1 def Show (*arg):2     print(Arg,type (ARG))34 Show ( 11,22,'ee')

The results of the implementation are as follows:

' ee ') <class'tuple'>

Case where 0 parameters are passed

1 def Show (*arg):2     print(Arg,type (ARG))34 Show ()

The results of the implementation are as follows:

() <class'tuple'>
Keyword parameters

When defining a function, you can add two * numbers before the parameter. If you add a * * number, you can pass multiple arguments like a function, and they will be handled internally as a dictionary.

As with the above, when calling this function, you can pass any parameter, including 0 parameters.

The function's invocation format is such a function name (keyword = value)

1 def Show (* *Arg):2     print(Arg,type (ARG))34 Show (n1='N1', n2=89)

The results of the implementation are as follows:

{'N1'N1'n2': <  Class'dict'>

Cases where 0 parameters are passed in:

1 def Show (* *Arg):2     print(Arg,type (ARG))3 4 Show ()

The results of the implementation are as follows:

{} <class'dict'>
Variable parameter and keyword parameter mates

Variable parameters and keyword parameters can be used together. After their use, it can be said that God has blocked the killing of the Buddha.

1 def Show (*args,**Kwargs):2     print(Args,type (args))3     Print(Kwargs,type (Kwargs))45 Show (11,22,'ee  ', n1='rr', s1=33)

The results of the implementation are as follows:

' ee ') <class'tuple'>{'N1'  rr's1': <class'  Dict'>

However, they are used in combination with a small temper, and the combination must satisfy a few two conditions:

  1. When defining a function, the variable parameter must precede the keyword argument

2. Call the function, the data of the variable parameter must be in front of the data of the keyword parameter, can not confuse or reverse the position, otherwise the program will error

Parameters of the python3.x function

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.