Python parameter type

Source: Internet
Author: User

http://cage.8cuo.org/?p=115

Python's parameter types can be grouped into 4 categories: 1. Position parameters 2. keyword parameter 3. Tuple parameters 4. Dictionary parameters
First, Position parameters:
Features: Unlimited number of calls must provide the number and position are the same as the formal parameters of the argument;
Sample code:
#函数声明
#第三个参数要能转化为数字, or the error will be
DEF fn (a,b,c):
sum = a + B + C
num = int (c);
#函数调用
FN (1,2,3) #正确的调用
fn (1) #错误的调用
fn (1,2, ' ASD ') #错误的调用

Second, keyword parameters
Features: Unlimited number, no need to care about the order of parameters, when the corresponding parameter is not passed, there is a default value for the function to use.
Sample code:
#函数声明
DEF fn (A = 1,b = 2, c = 0):
Print a
Print B
Print C
#函数调用
FN () #正确, a = 1, b = 2, c= 3
FN (10,11,12) #正确, a = ten, B = one, c = 12
fn (10,b =, c =) #正确, a = ten, B = c= 30
fn (10,c =, B = #正确, a = 10,b = 21
fn (c = 2, B = 4, a = 7) #正确, a = 7, B = 4, c = 2
fn (A = 2l, C =8) #错误, the first argument passed by the function does not use a keyword. The default is passed to the first formal parameter of the function declaration. A value is passed again to the keyword parameter A, which triggers Python's typeerror:multiple values for keyword argument ' a ', that is, the keyword parameter A has more than one value
fn (A = 12,1,4) #错误, the keyword parameter, whether declared or called, should be after the position parameter

Three, tuple parameters
Features: The number of parameters is not fixed (call is not required to know the need to pass a few parameters), the number of arguments can be 0, can also be countless.
Sample code:
#函数声明
DEF fn (*args):
For i in args:
Print I
#函数调用
FN (1,2,3) #正确, printed separately 1,2,3
FN (* (1,2,3)) #正确, print out 1,2,3
Note: When the parameter of a function has a * flag, Python handles the incoming arguments internally, i.e. converting the obtained arguments to a tuple (tuple). For the first call in the example above, there are two steps to understand:
Step1: The function received three arguments 1,2,3
Step2: Use these 3 arguments to form a tuple
In the second call, the parameter received by the function is itself a tuple, but the call is to prefix the actual participation of the tuple type to indicate that this is a tuple parameter

Four, dictionary parameters
Features: Similar to tuple parameters, but unlike tuple parameters, dictionary parameters are equivalent to an unfixed keyword parameter, which is equivalent to an indefinite number of positional parameters.
Sample code:
#函数声明
DEF fn (**keyargs):
For k,v in Keyargs.items ():
Print K + "=>" + V
#函数调用
fn (A = 2, B = 3, c = 4) #正确, print out a => 2,b = > 3, c => 4
fn (**{' a ': 2, ' B ': 3, ' C ': 4}) #正确, print out a => 2,b = > 3, c => 4
Description: Similar to the tuple parameter, the function will receive the parameter in the form of key => value to form a dictionary for use within the function
The last thing to note:
When several types of parameters are mixed, note the order in which they are written, in the correct order:
Positional parameter-keyword parameter-tuple or dictionary parameter

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.