Function (as defined in, or absence of the return value, parameters and parameter passing problem)


1. Definitions
DEF Test ():
X + =. 1
return X
benefits: reduce the code reuse *
* consistency and ease of maintenance
* Scalability
2. Have on Return Value
None: no return value is a function of the process
are: a ---- return itself
DEF Test ():
S = [5,4,32,556,22]
return S
Print (Test ())
# print the results [5, 4, 32, 556, 22]
a plurality ---- Back tuples
DEF Test ():
L = [5,4,32,556,22]
S = 'fjy'
return L, S
Print (Test ())
## print result ([5, 4, 32, 556, 22], 'fjy')
3. Parameter 
human form will be performed only when the reference calls, compile only be encountered.
A function to return a hit end
1. Location parameters must correspond indispensable
DEF Test (X, Y, Z):
Print (X)
Print (Y)
Print (Z)
Test (l, 2,3)
2. keyword parameter correspondence need, not a lack of a multiple nor
DEF Test (X, Y, Z):
Print (X)
Print (Y)
Print (Z)
Test (Y =. 1, X =. 3, . 4 = Z)
3. mixture of position parameter must be left keyword parameter
test (1, y = 2,3) # error
test (1,3, y = 2) # error
test (1,3, z = 2 )
****** a two parameter values can not pass
test (1,3, z = 2, y = 4) # error
4. parameters: ** Dictionary - keyword parameters
* neuron progenitor - a variable parameter
(* traversal means, a plurality of printing parameters is converted into tuples)
DEF Test (X, * args):
Print (X)
Print (args)
test (1) # print result:. 1
Test (1,2,3,4,5) # print result:. 1 (2,. 3,. 4,. 5)
Test (. 1, { 'name': 'Alex'}) # Print results:. 1 ({ 'name': 'Alex'},)
Test (. 1, [ 'X', 'Y', 'Z']) # print result: 1 ([ 'x', 'y', ' Z '],)
Test (. 1, * [' X ',' Y ',' Z ']) # print result:. 1 (' X ',' Y ',' Z ')
Test (. 1, * (' X ',' y ',' z ')) # print result:. 1 (' X ',' Y ',' Z ')

DEF Test (X, kwargs **):
Print (X)
Print (kwargs)
Test (. 1 , y = 2, z = 3 ) # print result:. 1 { 'Y': 2, 'Z':}. 3
Test (. 1, ** { 'Y':. 5, 'Z':}. 3) # results: {. 1 'Y':. 5, 'Z':}. 3
** here only key string
 

Guess you like

Origin www.cnblogs.com/snowony/p/11741566.html