python: parameter transfer function method demonstration

"" " 
Parameter transfer function method demonstrates 
" "" 
# 1 position parameter passing 
DEF Normal (A, B, C):
     Print ( " 1. Location parameter passing: " )
     return A, B, C 

Print (Normal (. 1 , 2, 3 ))
 # return return value: 
# return nothing to write or simply do not write return. None is then returned 
# to write back a return value, then the caller may receive a result 
# return later wrote more than one result, the caller can receive a tuple, the caller can be deconstructed into multiple variables directly 
# so the results presentation tuple is in the form of, ** kwords parameter passing except 

# 2 keyword parameter 
DEF Keys (a, B, C):
     Print ( " 2. parameter passing keywords: " )
     return a, B, C

Print (Keys (A =. 1, c = 2, B =. 3))   # where a, b, c of the variable sequence, the parameters may not be separated by spaces equal sign. 

# 3 position parameter and key parameters mix 
DEF nor_key (A, B, C):
     Print ( " 3. position parameter and key parameters in combination: " )
     return A, B, C 

Print (nor_key (. 1, B 2 =, =. 3 C))   # positions before the keyword parameter to parameter 

# 4 * number parameter passing: first want to transfer a lot of parameters, using the form "* a" form 
# within the function call only when It needs to be called "a" can 
DEF more_argu (* A):
     Print ( " 4. No transfer * reference: " )
     return A 

Print (more_argu (. 1, 2,. 3, " A " ," Haha " , [. 1, 2, 3,4-], { " nice day " , 123}, { " name " : " violent little cute " , " City " : " Nanjing " , " A " :. 1 }) ) 

# 5. use of positional parameters / keyword parameter / * number with 
DEF nor_key_more (A, B, * C, D):
     Print ( " The position parameter / keyword parameter / parameter passing an asterisk: " )
     return A, B, C, D 

Print (nor_key_more (. 1, 2,. 1, 2,. 3, "a", " Haha " , [. 1, 2, 3,4-], D = 2)) # positional parameters * Prior to, after the keyword arguments * 

# . 6 Kwords **:. The form "** kwords" form of mass parameters, upon receiving function, will be automatically converted to the key-value pair (dictionary) form 
# when transmission parameters, must also pass the form "A =. 1, B = 2, C =. 3" 
DEF OTHER (** A):
     Print ( " . 6 Kwords parameter passing **:. " )
     return A 

Print (OTHER (A =. 1, B = 2, C =. 3, D = 456, E = (. 1, 2,. 3)))

python: parameter transfer function method demonstration

Results are as follows

1 Location pass parameters: 
( 1, 2, 3 )
 2 . Keywords transmission parameters: 
( 1, 3, 2 )
 three positional parameters and key parameters in combination: 
( 1, 2, 3 )
 4. * Number pass reference: 
( . 1, 2,. 3, ' A ' , ' haha ' , [. 1, 2,. 3,. 4], { ' nice day ' , 123}, { ' name ' : ' violent small cute ' , ' City ' : ' Nanjing ' , ' A ' :. 1 })
The position parameter / keyword parameter / * Number parameter passing: 
( . 1, 2, (. 1, 2,. 3, ' A ' , ' haha ' , [. 1, 2,. 3,. 4]), 2 )
 6. The * * Kwords parameter passing: 
{ ' A ' :. 1, ' B ' : 2, ' C ' :. 3, ' D ' : 456, ' E ' : (. 1, 2,. 3)}

Slowly feel the brain is not enough, want to stick to it every day, little by little. Elaborate write as much as possible, to be a note,

Guess you like

Origin www.cnblogs.com/vvrr/p/11258331.html