Learning Python function acquaintance two

I. Introduction argument between Form

Parameter: parameter called the formal parameters defined in the definition phase function, referred to as parameter, corresponding to the variable name.
Argument: the incoming call function called actual parameter value stage, referred to as the argument, equivalent to the value of the variable. 

Relations between Form argument:

  1, calling stage, the argument (variable value) will be bound to the parameter (variable names).

  2, this binding relationship can only be used in the body of the function.
  3, binding relationship between the actual parameter involved in the function call to take effect after the end of the function call unbind.

Value argument is passed, but the value may be in the form:
# Form a: 
FUNC (1,2 ) 

# form two: 
A =. 1 
B = 2 
FUNC (A, B) 

# form three: 
FUNC (int ( ' . 1 ' ), 2 ) 
FUNC (func1 ( 1,2,) , func2 (2,3), 333)

 

II. Participation type specific use arguments
2.1 positional parameters: left to right in the order according to the parameter defined position parameter called 
 
the position parameter: function definition phase in accordance with the "variable name" from left to right in the order defined directly
 characteristics: values must be transferred, not one more nor one less.
DEF FUNC (X, Y):
      Print (X, Y)
 # FUNC (l, 2,3) # Error 
# FUNC (. 1,) # Error
 Location argument: the call phase function, in accordance with the order and some from the left to the incoming value. 
 Features: parameter correspondence with the order.
def func(x,y):
     print(x,y)
func(1,2)    #x=1,x=2
func(2,1)    #x=2,x=1
 
2.2 Keyword parameters
Keyword argument: the function call phase, in the form of key = value of the passed-in value
Features: by name to pass a parameter value, the reference sequence can be completely
DEF func (X, Y):
     Print (X, Y) 

func (Y = 2, X = 1 ) 
func ( 1,2 ) 

# mixture, emphasizes 
# 1, the position must be placed before the keyword argument argument 
func (. 1, Y = 2 ) 
FUNC (Y = 2,1 ) 

# 2, can not pass can be repeated for the same parameter value 
FUNC (. 1, Y = 2, X =. 3 ) 
FUNC ( 1,2, X =. 3, y = 4)

 

2.3 default parameters
Default parameter: function definition phase, has been assigned a parameter, called the default parameters
Features: During the definition phase has already been assigned, meaning that the call can not assign a value to stage
 
DEF FUNC (X, Y =. 3 ):
     Print (X, Y) 

# FUNC (X =. 1) 
FUNC (X =. 1, Y = 44444 ) 


DEF Register (name, Age, Gender = ' M ' ):
     Print (name , Age, Gender) 

the Register ( ' three guns ' , 18 ) 
the Register ( ' artillery ' , 19 ) 
the Register ( ' cannon ' , 19 ) 
the Register ( ' no gun ' , 19, ' female ' )
 
# Position between Form default parameter mix, stressed: 
# 1, the position parameter must be in the default parameter left 
DEF FUNC (Y = 2 , X):
     Pass 

# 2, the default parameter values are assigned in the function definition phase , is given precisely the value of the memory address 
# model. 1: 
m = 2 DEF FUNC (X, Y = m): # Y => 2 memory address Print (X, Y 
m = 3333333333333333333 
FUNC ( . 1 ) # model 2: 
m = [111111 ,] DEF FUNC (X, Y = m): # Y => [111111,] memory address Print (X, Y) 
m.append ( 3333333 ) 
FUNC ( . 1 ) # . 3, although the default value can be specified as any data type, but does not recommend the use of variable types

    




    



# Function Ideal state: call the function only has a relationship with the function itself, without external influence code 

DEF FUNC (the X-, the y-, z, L = None):    # Recommended 
    IF L IS None: 
        L = [] 
    l.append (X) 
    l.append (Y) 
    l.append (Z) 
    Print (L)
 

2.4 variable parameter length (* and ** usage)

of variable length refers to the function call, the number of values (argument) is not fixed incoming. Argument is used to the parameter assignment, the corresponding, actual parameters for the overflow must be received corresponding parameter.

2.4.1 position parameter variable length
The I: * parameter name: overflow position for receiving arguments, the position of the overflow * real participants are saved as a tuple format then assigned immediately following parameter name 
      * heel may be any name, but the convention it should be args.

def func(x,y,*z): # z =(3,4,5,6)
    print(x,y,z)
func(1,2,3,4,5,6)

def my_sum(*args):
    res=0
    for item in args:
        res+=item
    return res
res=my_sum(1,2,3,4,)
print(res)

II: * can be used in the arguments, the argument with *, the value of the first argument * broken into position.
Def function (x, y, z):
     print (x, y, z) 

# operation (* [11,22,33]) # operation (11,22,33) 
# operation (* [11,22]) # function (11.22) 

l = [11,22,33 ] 
func ( * l)

III: Types of actual parameters are marked with *
 
def func(x,y,*args): # args=(3,4,5,6)
    print(x,y,args)

func(1,2,[3,4,5,6])
func(1,2,*[3,4,5,6]) # func(1,2,3,4,5,6)
func(*'hello') # func('h','e','l','l','o')
 
Keyword parameters 2.4.2 variable length
 
The I: ** parameter names: for receiving spilled Keyword argument, ** spill-over into a dictionary keyword argument save format, and then assigned to the immediately following parameter name 
** may be followed by any name, but the convention should be kwargs
def func(x,y,**kwargs):
    print(x,y,kwargs)

func(1,y=2,a=1,b=2,c=3)
 

II: ** may be used in the argument (** heel only dictionary), with the arguments *, ** value of the first argument broken into Categories
def func(x,y,z):
    print(x,y,z)

func(*{'x':1,'y':2,'z':3}) # func('x','y','z')
func(**{'x':1,'y':2,'z':3}) # func(x=1,y=2,z=3)

#错误
func(**{'x':1,'y':2,}) # func(x=1,y=2)
func(**{'x':1,'a':2,'z':3}) # func(x=1,a=2,z=3)
 
III: Types of actual parameters are with **
def func (x, y, ** kwargs):
     print (x, y, kwargs) 

func (and = 222, x = 111, a = 333, b = 444 ) 
func ( ** { ' and ' 222, ' x ' : 111 ' to ' 333, ' b ' : 4444})

 

# Mix * and **: * args must ** kwargs before 
DEF FUNC (the X-, * args, ** kwargs):
     Print (args)
     Print (kwargs) 

FUNC ( 1,2,3,4,5,6, 7, 8, X =. 1, Y = 2, Z =. 3 ) 


DEF index (X, Y, Z):
     Print ( ' index = >>> ' , X, Y, Z) 

DEF warpper (* args, ** kwargs): # args = (. 1,) = {kwargs 'Z':. 3, 'Y': 2} 
    index (* args, ** kwargs)
     # index (* (. 1,), ** { 'Z': . 3, 'Y': 2}) 
    # index (. 1,. 3 = Z, Y = 2) 

(wrapper . 1, Z =. 3, Y = 2) # parameter is passed to the wrapper with index 
# original format - - "summary -----" show their colors

Guess you like

Origin www.cnblogs.com/imark7/p/12519802.html