Python function parameter types and usage skills

Let's first define a function to calculate body mass index (BMI). Body mass index is the ratio of weight to the square of height, where weight is in kilograms and height is in meters.

>>> def bmi(height, weight, name):
	i = weight/height**2
	print('%s的体重指数为%0.1f'%(name, i))

	
>>> bmi(1.75, 75, 'Xufive')
Xufive的体重指数为24.5

The custom function bmi has three parameters, and each parameter has a clear meaning. When calling this function, these three parameters must be passed in in the defined order, none of which is indispensable. This is also the most basic parameter passing rule for Python functions.

Next, modify the bmi function a bit and specify a default value for the name parameter.

>>> def bmi(height, weight, name='您'):
	i = weight/height**2
	print('%s的体重指数为%0.1f'%(name, i))

	
>>> bmi(1.75,75) # 可以不传递name参数,使用默认值
您的体重指数为24.5
>>> bmi(1.75,75,'Xufive') # 也可以传递name参数
Xufive的体重指数为24.5

Now the bmi function has two types of parameters: weight and height, which are indispensable parameters when the function is called, and the order must be consistent with the definition of the function. Such parameters are called 位置参数; name is optional when the function is called Optional parameters (if not provided, the default value is used), such parameters are called 默认参数. There can be multiple default parameters.

In order to make the results more accurate, you can consider using the average of multiple weights in the most recent period to calculate the body mass index, which means you need to enter multiple weight values. Of course, we can pass a weight tuple or list to weight, but in this way weight and height are not the same type. The aesthetics are lost in form, and it is easy to misunderstand when using it. The best way is to allow the function to accept an indeterminate number of weight parameters.

>>> def bmi(height, *args, name='您'):
	i = (sum(args)/len(args))/height**2
	print('%s的体重指数为%0.1f'%(name, i))

	
>>> bmi(1.75, 75, name='xufive')
xufive的体重指数为24.5
>>> bmi(1.75, 75, 74)
您的体重指数为24.3
>>> bmi(1.75, 75, 74, 75.5, 74.7, name='xufive')
xufive的体重指数为24.4

In fact, there is no problem calling functions like the following.

>>> weight = [75, 74, 75.5, 74.7]
>>> bmi(1.75, *weight, name='xufive')
xufive的体重指数为24.4

This is a bit more complicated. The bmi function has three types of parameters. In addition to positional parameters and default parameters, there is one more 可变参数, that is , the bmi function can accept an unlimited number of parameters. When the function is defined, the variable parameter name is prefixed with "*"; in the function body, the variable parameter is equivalent to a tuple.

As a result, a new question arises: In what order should the three types of parameters be defined? There is no objection to the positional parameter ranking first. In principle, the default parameter is placed last, but the parameter name must be added when calling (as in the above example), otherwise the function cannot distinguish whether it is a variable parameter or a default parameter. The default parameter can also be placed before the variable parameter (this is not recommended), but the parameter name cannot be used when calling, even if the default value is used, the parameter cannot be omitted, otherwise the function will use the first value of the subsequent variable parameter (if If there is, it is forced to assign a value.

Let's talk about more complicated situations. In addition to the above-described three types of parameters, Python supports a fourth type of function parameters: 关键字参数. Keyword parameters consist of an unlimited number of key-value pairs. When the function is defined, the keyword parameter name is prefixed with "**"; in the function body, the keyword parameter is equivalent to a dictionary.

>>> def bmi(height, *args, name='您', **kwds):
	i = (sum(args)/len(args))/height**2
	print('%s的体重指数为%0.1f'%(name, i))
	for key in kwds:
		print('%s的%s是%s'%(name, key, str(kwds[key])))

		
>>> bmi(1.75, 75, 74, 75.5, 74.7, name='Xufive')
Xufive的体重指数为24.4
>>> bmi(1.75, 75, 74, name='Xufive', 性别='男', 爱好='摄影')
Xufive的体重指数为24.3
Xufive的性别是男
Xufive的爱好是摄影
>>> bmi(1.75, 75, 74, 性别='男', 爱好='摄影', name='Xufive')
Xufive的体重指数为24.3
Xufive的性别是男
Xufive的爱好是摄影
>>> bmi(1.75, 75, 74, 75.5, 74.7, 性别='男', 爱好='摄影')
您的体重指数为24.4
您的性别是男
您的爱好是摄影

If a function has the above four types of parameters at the same time, the correct order of the parameters when defining the function should be positional parameters in the first place, variable parameters second, default parameters again, and keyword parameters at the end. When the function is called, if you need to specify the default parameter, the parameter name cannot be omitted, the position is after the variable parameter, it can be before or after the keyword parameter, or it can be mixed.

Guess you like

Origin blog.csdn.net/xufive/article/details/115298728