How to pass parameters in Python, these six methods must be mastered

Any programming language function is a very important part, and when making function calls, it is necessary to understand how parameters are passed to functions. What parameter passing methods are supported in Python?

The parameter passing method in Python is relatively flexible, mainly including the following six:

  • Pass parameters by location

  • Pass parameters by keyword

  • default parameters

  • tuple parameter

  • dictionary parameter

  • Comprehensive reference

Pass parameters by location

As the name implies, the actual parameters are consistent and correspond one-to-one by position.

def func(a, b):  
    return a+b*2

When the function func(1,2) is called, the actual parameter corresponds to the formal participation in the form of a=1, b=2, and the return result is 5.

Pass parameters by keyword

In this way, both actual and formal parameters are passed when calling a function. Therefore, there is no need to correspond according to the position, the position can be reversed:

def func(a, b):  
    return a+b*2

When the function func(b=1, a=2) is called, the actual parameter corresponds to the formal participation in the form of b=1, a=2. The matching method is the keyword instead of the position, and the return result is 4.

default parameters

The default parameters must be placed after the first two types of parameters, which can be passed or not passed when calling the function. If not passed, the default parameter value will be used.

def func(a, b, c = 3):  
    return a+b*2+c

Among them, c is the default parameter. When the function is called through func(1,2), the value of c is the default value of 3, and the execution result of the function is 8; when the function is called through func(1,2,5), the value of c is the actual parameter value 5. The execution result of the function is 10.

tuple parameter

The incoming parameters are presented in the form of tuples, and the length is not limited. You can access each input parameter in turn through the tuple access method:

def func(*args):  
    for eacharg in args:  
        print ('tuple arg:', eacharg)

The calling method is as follows: func('I', 'Love', 'PythonSomething')

The result of the call is:

tuple arg: I  
tuple arg: Love  
tuple arg: PythonSomething

dictionary parameter

The incoming parameters are presented in the form of a dictionary, and the length is not limited. You can access each input parameter in turn through the access method of the dictionary:

def func(**kwargs):  
    for eachkwarg in kwargs.keys():  
        print('Dict Arg', eachkwarg, ':', kwargs[eachkwarg])

The calling method is as follows:

func(English_name = 'PythonSomething', Chinese_name= 'PythonSomething')

The result of the call is:

Dict Arg English_name : PythonSomething  
Dict Arg Chinese_name : Python那事

Comprehensive reference

It is a combination of the above-mentioned methods. In fact, you can also find from the above example that tuple parameter passing and dictionary parameter passing can already satisfy most parameter passing scenarios. Therefore, the comprehensive parameter transfer usually refers to the combination of these two, and you will often find them in the source code:

def func(*args,**kwargs):  
    for eacharg in args:  
        print('tuple arg:', eacharg)  
  
    for eachkwarg in kwargs.keys():  
        print('Dict Arg', eachkwarg, ':', kwargs[eachkwarg])

The calling example is:

func(‘I’, ‘Love’, ‘PythonSomething’, English_name = ‘PythonSomething’, Chinese_name= ‘Python那事’)

The result of the call is:

tuple arg: I  
tuple arg: Love  
tuple arg: PythonSomething  
Dict Arg Chinese_name : Python那事  
Dict Arg English_name : PythonSomething

Guess you like

Origin blog.csdn.net/m0_59596937/article/details/127273990