Eleven, python basis: Usage function of various parameters

Eleven, python basis: Usage function of various parameters

1. Why have the function parameters?

We packaged as a function of the purpose it is to make it reusable, if you want your function to adapt to more cases, we will set aside the parameters to a function that allows the user's own incoming parameters, processing requirements under different conditions .

2. What types of function parameters?

In python, a function of the parameters can be divided into the following categories:
positional parameters, keyword parameters, the default parameter, multi-value parameter

2.1, location parameters

def get_info(name, age, height):
    print(name)
    print(age, height)

This function above get_info There are three parameters: name, age, height.

They are not positional parameters, depending on the time you call, it is how to pass the Senate.

For example:
When we look at the call, according to the positional parameters to pass argument is kind of how

def get_info(name, age, height):
    print(name)
    print(age, height)
    
get_info('小明', 12, 180)    # 正确调用方式
get_info(12, '小明', 180)    # 错误调用方式,位置错了
get_info('小明', 12)     # 错误调用方式,参数缺少了

Positional parameters Note:
1, according to the positional parameters when passed the Senate, must pay attention to the wrong location can not otherwise function to get the data is not right.
2, the number of positional parameters is no more and no less, there are 3 position parameters defined function, call the function of time, it must pass three parameters.
3, the definition of a function, no matter how many kinds of parameters, positional parameters are always put in front of other types of parameters.

2.2, keyword arguments

If you do not want in order to position the function parameter passing, you can use keyword arguments passed parameters.

def get_info(name, age, height):
    print(name)
    print(age, height)
    
get_info(age=12, name='小明', height=180)

According to this way of mass participation of key-value pairs, even if you order the wrong location does not matter, as long as the keywords written on the line, look at the implementation of the results:
Here Insert Picture Description

2.3, the default parameters (default parameters)

The default parameter is also called the default parameters, that is, when you pass parameters, you can not pass this parameter, the function itself will set a default value for it, you do not pass parameters to the default values.

A parameter is not the default parameters, not on the way to pass parameters when you call, but depends upon the definition, there are no parameters to the default values.

We change it a little above function, into this below, to the height a default value of 180, when you call it, we can pass parameters, you can not pass, look at the results

def get_info(name, age, height=180):
    print(name)
    print(age, height)
    
get_info(age=12, name='小明')
get_info(age=12, name='xiaoming', height=170)

Here Insert Picture Description
Or normal output, when you call if there is a default parameters to pass parameters, it uses the parameters you pass, if not, the default value is set when the original definition.

Note: The default parameter must be after the position parameters, we show you, to age a default value, in the middle, to see what works.

def get_info(name, age=12, height):
    print(name)
    print(age, height)
    
get_info(12, 170)

The results will be given:
Here Insert Picture Description
Here Insert Picture Description

2.4, multi-parameter

Sometimes when we defined functions, allowing users to transfer many parameters, but the number of parameters is uncertain, this time you can use a multi-value parameter.

There are two kinds of multi-parameter representation, corresponding to different types of data transmission:

* args represents receiving a tuple, the tuple of length is not limited;
** kwargs represents receiving a dictionary, the dictionary is not limited in length;

def get_info(name, age, *args, **kwargs):
    print(name, age)
    print(*args)
    print(**kwargs)
    
t = (123, 345)
d = {'phone': '156XXX'}
get_info('小明', 12, t, d)

Look at the results:
Here Insert Picture Description
The results look seemingly did wrong, right.
But, in fact, it is a problem! ! !
Look at its output, we obviously set up three print (), print out the result should be three lines in a function, but why only output the results of two lines? But it did not print out the contents missing, all output.

There is little need special attention because of the problem caused by the above, is the interpreter you pass two parameters t and d are collected inside * args, ** kwargs but did not collect parameters, therefore, out of print only 2 lines.

Solution:
When parameter passing, respectively, to two parameters plus symbols * and ** Another point to note here is that print (** kwargs), wanted to change the print (kwargs) or print (* kwargs) , otherwise it will error.

def get_info(name, age, *args, **kwargs):
    print(name, age)
    print(*args)
    print(*kwargs)
    
t = (123, 345)
d = {'phone': '1560000'}
get_info('小明', 12, *t, **d)

Look at the output:
Here Insert Picture Description
Here Insert Picture Description
a function of the parameters of advice:
pay attention to is best not to use a variable type of data container, as the default parameter, which is a list of these dictionaries, though you will not complain it as the default parameters, but sometimes can cause some strange The problem.

The problem I have another article specifically said:

python pit: Use the empty list as the default parameter, so I doubt encountered a supernatural codes

Published 51 original articles · won praise 76 · views 9098

Guess you like

Origin blog.csdn.net/Jacky_kplin/article/details/104810354