Twenty-four variable-length parameters

Twenty-four variable-length parameters

First, prior knowledge

  • 1. The variable length parameters: refers to the function call, the number of parameters passed may not fixed == ==
  • 2. When calling a function, the value of no more than two pass mode, one is the position of the argument, and the other argument is a keyword, and therefore must have formal parameters there are two solutions, to receive the overflow from the position of the real participate keyword arguments

## two variable length parameter

1. The variable length parameter of *

  • * Parameter will overflow the argument-all positions, in the form of tuples stored, then the name assigned to the parameter tuple after *. * General parameters behind the convention was args
def func(*args):        # args = (2,4,6,8,10)
    num = 0
    for i in args:
        num += i
    return num

sum_int = func(2,4,6,8,10)
print(sum_int)            # 30

2. The variable length parameter **

  • 1. The parameter ** spill-over-all keyword arguments, stored as a dictionary, and the dictionary assigned to the ** parameter name after. ** parameter name after the general convention is kwargs
def func(**kwargs):
    print(kwargs)
    
func(a=5,name = 'king')    # {'a': 5, 'name': 'king'}
  • 2. The need to understand keyword argument is actually in the form of a variable name = variable value, then passed to the function key will be stored in the form of

Third, the variable-length argument

1. The variable length argument *

  • The arguments * arguments will be broken into its rear position argument == == cycle and removed. After arguments whenever encountered with *, * represents a position behind the argument, into a position immediately broken up parameters
def func(x,y,z,*args):
    print(x,y,z,args)
    
func(*(1,2,3,4),5,6,7)   # 先打散成(1,2,3,4,5,6,7)
# 然后传给形参就变成 x=1,y=2,z=3,args=(4,5,6,7)
# 所以最后的结果是:1 2 3 (4, 5, 6, 7)

2. The variable length argument **

  • The argument ** will be broken into its subsequent arguments == == Keyword argument cycle and removed. After the encounter argument whenever the tape **, it means that it is the key argument, immediately broken into keyword arguments
def func(x,y,**kwargs):
    print(x,y,kwargs)
    
func(2,3,**{'name':'king','age':20}) 
# 2,3,{'name':'king','age':20}

Fourth, the application vararg

  • Parameter in: * indicates a receiving position parameter overflow, ** represents the keyword parameters received overflow

  • In arguments: * indicates the position parameter transfer, ** represents the keyword parameter transfer

def f1(name,age,gender):
    print(f'my name is {name},my age is {age},my gender is {gender}')
          
def f2(*args,**kwargs):
          print(f'args = {args}, kwargs = {kwargs}')
          f1(*args,**kwargs)
f2(age =20, gender ='male', name ='king') 

'''
args = (), kwargs = {'age': 20, 'gender': 'male', 'name': 'king'}
my name is king,my age is 20,my gender is male
'''

Five key parameter named

  • 1. When a predetermined logic function arguments must follow the keyword parameter passing, it must be specified by value Keyword argument function as required
def f(x,y,**kwargs):
    if 'name' not in kwargs or 'age' not in kwargs:
        print('name和age必须用关键字实参进行传值')
        return
    print(kwargs['name'],kwargs['age'])
    
f(1,3,name='king',age=20)
# king 20
  • 2. In the definition phase function, * the latter parameters are key parameter
def f(i, *, name, gender='male', age):
    print(i)
    print(age)


f(1, x='king', age=19)  '''TypeError: register() got multiple values for argument 'x' 。也就是关键字实参传值时不能随意改关键字形参的key'''
  • 3. keyword to pass parameter values, must follow the key = value form of traditional values, and key name must be specified parameter name keyword
def f(i, *, name, gender='male', age):
    print(i)
    print(age)


f(1,'king', age=19) 

VI Summary

def func(*args,**kwargs):
    code
  • The wording of the above, no matter how many positional parameters or keyword parameters passed, you can receive all

Guess you like

Origin www.cnblogs.com/itboy-newking/p/10953488.html