Python function parameter Summary

Source: Internet
Author: User

As a person who has never studied python in a system, but has to look at the source code on the day of that day, Nima is suffering... I often see ** KWS and so on. What is this? Pointer pointer ?! T ^ t

When I read "Python core programming" Yesterday, I just saw the function section. So I would like to summarize all the currently available concentrated parameter types ^

In general, except normal ARGs, special function parameters in Python include: keyword parameter, default parameter, non-Keyword variable length parameter (tuples) keyword variable length parameter (dictionary), which is described in the following example.

Keyword Parameter: The parameter name is specified during the call and consistent with the parameter name when the function is declared. When using keyword parameters to allow function calls, the order of parameters is different from that when the function is declared. Values are assigned only based on the parameter settings. Example:

 #  Function Definition  Def  Foo (x, y ):  Print   '  X is % s  ' % X  Print  '  Y is % s  ' % Y  If   _ Name __ = '  _ Main __  '  :  #  Standard call Foo (1, 2 )  #  Keyword call Foo (y = 1, x = 2)

In standard calls, X and Y are 1 and 2 in sequence. In keyword calls, the values of X and Y are specified based on their names and are irrelevant to their order.

#Standard call outputXIs1YIs2#Keyword call outputXIs2YIs1

 

Default parameters: When declaring a function, specify the default value of the parameter. You do not need to change the parameter (use the default value) when calling the function ). Example:

DefTax (cost, rate = 0.17):PrintCost * (1 +Rate)If _ Name __='_ Main __':#The default value of rate is 0.17.Tax (1000)#Rate is 0.05Tax (1000, 0.05)

When the default value is used, the rate is 0.17 and the result is 1170. When the parameter rate is specified, the rate is 0.05 and the result is 1050.

#Tax (1000) Output1170.0#Tax (1000, 0.05) Output1050.0

 

Variable Length Parameter (tuples): "Non-keyword" and "variable length", as the name suggests, allow multiple "non-keyword" parameters to be input during the call. python will put these parameters into a single tuple. Example:

 #  Defines the function. * therest is a non-Keyword variable length parameter.  Def Tuplevarargs (arg1, arg2 = '  Defaultb  ' , *  Therest): Print   '  Formal Arg 1:  '  , Arg1  Print   '  Formal Arg 2:  ',  Arg2  For Eachxtrarg In  Therest:  Print   ' Another Arg:  ' , Eachxtrarg

We use a variety of call methods to view the results, so as to understand the use of non-Keyword variable length parameters:

>>> Tuplevarargs ( '  ABC  '  ) Formal ARG 1 : Abcformal ARG 2 : Defaultb
>>> Tuplevarargs ( ' ABC ' , ' Def ' ) Formal ARG 1 : Abcformal ARG 2: Def
>>> Tuplevarargs ( ' ABC ' , ' Def ' , ' XYZ ' , 123.4 ) Formal ARG 1 : Abcformal ARG 2:Def Another Arg: xyzanother Arg: 123.4

 

Variable Length keyword parameter (dictionary): "Keyword", "variable length", as the name suggests, allows multiple "keyword" parameters to be input during calls. python will add these multiple <parameter names, parameter value> put in a dictionary. Note that the keyword variable parameter should be the last parameter defined by the function, **. Example:

 #  Define the function. ** therest is the variable length parameter of the keyword.  Def Dictvarargs (arg1, arg2 = '  Defaultb  ' , **  Therest):  Print  '  Formal Arg 1:  '  , Arg1  Print   '  Formal Arg 2:  '  , Arg2  For Eachxtrarg In  Therest. Keys ():  Print   '  Xtra Arg % s: % s ' % (Eachxtrarg, STR (therest [eachxtrarg])

We use multiple call methods to view the results, so as to understand the use of variable-length keywords:

>>> Dictvarargs ( '  ABC  '  ) Formal ARG 1 : Abcformal ARG 2 : Defaultb >>> Dictvarargs ( '  ABC  ' , '  Def  ' ) Formal ARG 1 : Abcformal ARG 2: Def  #  A = 1 and B = 'aha' are variable keyword parameters, which are placed in therest dictionary. >>> Dictvarargs ( '  ABC  ' , '  Def  ' , A = 1, B = '  Aha  '  ) Formal ARG 1: Abcformal ARG 2: Def  Xtra Arg: 1 Xtra Arg B: Aha  #  All use keyword parameters, but only a and B are variable and will be placed in therest >>> Dictvarargs (arg2 = '  Def  ' , A = 1, B = '  Aha  ' , Arg1 = '  Put at the last  ' ) Formal ARG 1 : Put at the lastformal ARG 2: Def  Xtra Arg: 1 Xtra Arg B: Aha  #  The default value of arg2 is used this time. >>> Dictvarargs ( '  One  ' , A = 1, B = '  Aha  ' , Name = ( ' Yuki  ' , '  Lau  '  ) Formal ARG 1 : Oneformal ARG 2 : Defaultbxtra Arg: 1 Xtra Arg B: ahaxtra Arg name :(  '  Yuki  ' , '  Lau  ' )

 

Note:

When non-Keyword variable length parameters and keyword variable length parameters appear in the same function, they should follow the following sequence conventions:

 
DefNewfoo (normal_arg1, normal_arg2, * Non-keywords ,**Keywords ):Pass

Of course, you can also directly import tuples and dictionary objects to functions, such:

 
>>> Newfoo (2, 4, * (6, 8 ),**{'Foo': 10,'Bar': 12 })

You can also create a "mix and match". Isolated variable length parameters will be placed in the corresponding tuple or dict, for example:

 
>>> Newfoo (2, 4, 3, x = 4, y = 5, * atuple, ** adict)

In this example, parameter 3 is placed in atuple, and parameters x = 4 and Y = 5 are placed in adict.

 

Of course, there are still many questions about the parameters of Python functions. This article only introduces several common situations and provides a simple understanding and reference.

 

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.