parameter matching for Python functions

Source: Internet
Author: User

Version: General use python2.7.6

python3.4.3 will mark

1. Immutable objects (integers, strings) are passed through an object reference and cannot be changed inside the function.

1 def F (a): 2 ...     a=10034 >>> b=885 >>> f (b)6 >>> b 7 88

2. mutable objects (lists, dictionaries) are also passed through an object reference and can be changed inside the function. (The original object is changed locally, but the re-assignment does not change)

 1  >>> def   F (a):  2  ... a=[1,2,3 3   ...  4  >>> b=[]  5  >>> F (b)  6  >> > b  7  [] 
1 def F (a): 2 ...     A.append (3)34 >>> F (b)5 >>> b 6 [3]

3. Parameter matching

Default parameters and keyword parameters

1>>>defF (a):2... a.append (3)3 ... 4>>>F (b)5>>>b6[3]7>>>8 Keyboardinterrupt9>>>defF (a,b,c):Ten...Print(a,b,c) One ...  A>>> F (A) -(1, 2, 3) ->>> F (c=3,a=1,b=2) the(1, 2, 3) ->>>defF (a,b=2,c=3): -...Print(a,b,c) - ...  +>>> F (1,4) -(1, 4, 3)

4. Arbitrary parameter Matching

* and * * Support any number of parameters

* Collect parameters into a tuple.

1>>>defF (*arges):2...Print(Arges)3 ...4>>> F (1,'a',['2', 3],{'a': 1,'b': 2})5(1,'a', ['2', 3], {'a': 1,'b': 2})

* * Valid only for keyword arguments, pass keyword arguments to a dictionary, and then work in a dictionary.

1>>>defF (* *arges):2...Print(Arges)3 ... 4>>>F ()5 {}6>>> F (a:1)7File"<stdin>", Line 18F (a:1)9^Ten syntaxerror:invalid Syntax One>>> F (a=1,b=2) A{'a': 1,'b': 2} ->>> F (1=a,2=b) -File"<stdin>", Line 1 theSyntaxerror:keyword can'T is an expression ->>> F ('1'=a,'2'=b) -File"<stdin>", Line 1 -Syntaxerror:keyword can'T is an expression +>>> F (1='a', 2 ='b') -File"<stdin>", Line 1 +Syntaxerror:keyword can'T is an expression A>>> F ('a'=1,'b'=2) atFile"<stdin>", Line 1 -Syntaxerror:keyword can'T is an expression ->>> F (a=1,b=2) -{'a': 1,'b': 2} ->>> F (a=[1],b=2) -{'a': [1],'b': 2} in>>>

The form of multiple parameters can be passed

1>>>defF (*a,**b):2...Print(A, b)3 ...4>>> F (1,'a', [3,3],x=1,y='e')5((1,'a', [3, 3]), {'y':'e','x': 1})
1>>>defF (a,*b,**c):2...Print(a,b,c)3 ... 4>>> F ('s','s','s', s='s')5('s', ('s','s'), {'s':'s'})6>>>

The second and third ' s ' above are collected into a tuple

Only keyword parameters (Python3 or more) can be used.

1 def F (a,*b,c):2     ... Print (a,b,c) 3  4 >>> F (,'a', c=3)5'  a') 3
1 def F (a,*, b,c):2     ... Print (a,b,c) 3  4 >>> F (1,b='a', c=3)5 1 a 3

The first example of the keyword C after *b must be assigned using the C=3 equation method

The second example * after B and C must use the equation to assign the value of the way

parameter matching for Python functions

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.