The difference between a normal parameter, a specified parameter, a default parameter, and a dynamic parameter in a function

Source: Internet
Author: User

1. The normal parameter is the same as the parameters passed in the following function passed in the function, there is no default value
1 def F (a): 2     A = a + 13     return  a45 b=f (3)6Print (b)

2. Specify parameters

1 # If you pass in order like this, if you do not specify a value for the parameter, the A,B,C will be assigned the initial value in order . 2 d=f (3,4,5)3print(d)4# If you specify the value of a,b,c like this, Then they are the specified parameters that can be passed in their original order 5 e=f (c=5,b=4,a=3)6print(e)

3. Default parameters

1 #Default Parameters2 #To specify the initial value of B in parentheses like this, then B becomes the default parameter3 defF (a,b=9):4sum = a +b5     return(sum)6 7 #As can be seen here, we only pass in a parameter 3, it will be assigned by default in order to the first variable, a, B, is equal to the original default value of 98C=f (3)9 Print(c)Ten  One #when two parameters are passed, assigned to a, B, respectively, then A=3,B=4,B is re-assigned. AD=f (3,4) - Print(d)

4. Dynamic Parameters

1 #Dynamic Parameters2 #dynamic parameter *args means that when we need to pass in more than one parameter, we can use *args to represent multiple parameters, instead of specifying multiple parameters in parentheses separately .3 defF (*args):4     Print(Args,type (args))5 6D=f (A)7 8 #dynamic parameter **kwargs can be used when we need to pass in the parameter of the key value pair type **kwargs9 defF (* *Kwargs):Ten     Print(Kwargs,type (Kwargs)) One  AD=f (a=3,b=4) -  - #the parameters of the dynamic parameters of the universal mode, so that we can no matter how to pass the parameters of almost no error the defF (a,*args,**Kwargs): -     Print(A,args,type (args), Kwargs,type (Kwargs)) -  -D=f (22,33,44,k1=55,k2=66)

The difference between a normal parameter, a specified parameter, a default parameter, and a dynamic parameter in a 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.