Interpretation of the concept of different function parameters in python (positional parameters, default parameters, variable parameters, keyword parameters, named keyword parameters)

Interpretation of the concept of different function parameters in python (positional parameters, default parameters, variable parameters, keyword parameters, named keyword parameters, named positional parameters)

Here are python 函数参数some concepts and examples of recent review of relevant knowledge. For system learning content, please refer to teacher Liao Xuefeng’s notes.

1. Example

When we define a function, we often need to define the parameters of the function. Sometimes we do not understand what the various function parameters do. Here is a simple example:

def func(a, b=1, *args, **kwargs):
	print(a)
	print(b)
	print(args)
	print(kwargs)

From the above example,

  • a belongs to the default parameter
  • b is a positional parameter
  • Both args and kwargs are variable parameters. We will discuss the differences later.

2. Different parameter categories

1. Positional parameters

Parameters passed in sequentially in python, such as the following example:

def add(a, b):
	return a + b

implement:

print(add(5, 10))

result:

15

When we define adda function, the two parameters a and b accept parameters in order and assign values ​​to a and b. This type of parameter is the most common.

2. Default parameters

In python, if we want to set a default value and don't want to pass in this parameter every time, we can use the default parameter, such as the following example:

def power(a, n=2):
	print(a ** n)

implement:

power(2)
power(2, 2)
power(2, 10)

result:

4
4
1024

When we define powera function and find a^n, we more often want to use a^2, then we can set n as a default parameter. When passing in a parameter, n=2 will be used by default. Of course, we can also pass Enter two parameters, and n will be changed according to position.

Proper use of default parameters can reduce the complexity of the parameters passed in to our functions.

Notice:

  1. Required parameters must precede the default parameters
  2. Try to define parameters with large changes first.

3. Variable parameters

If we in python 输入参数的个数是可变的, then we need to use variable parameters at this time. For example, when we calculate add, if we don’t know how many parameters we want to pass in, we can use variable parameters:

def add(*nums):
	print(nums)
	print(sum(nums))

implement:

add(2, 3)
add(2, 2, 2)
add()

result:

(2, 3)
5
(2, 2, 2)
6
()
0

When we execute addthe function, *nums will assemble the incoming parameters into a tuple in order, and then provide them for us to use.

There will be a problem here, that is, if your parameter itself is a list or tuple, how to pass it in? At this time, you can use *nums to pass it in, as follows:

nums = [1,2,3]
add(*nums)

In this case, the three numbers 1, 2, and 3 in the nums list are passed in, and then used as a tuple.

4. Keyword parameters

In addition to the variable parameters, in addition to the required variable parameters, we may also need some non-required parameters. Using keyword parameters allows any number of parameters. When calling the function 组装成一个dict:

def example(a, b, **kw):
	print(name, age, kw)

implement:

example('123', 20, c=0, d=1)

result:

123 20 {
    
    'c': 0, 'd': 1}

Note:
It can be found that keyword parameters also allow any number of parameters to be passed in. The external parameters in the form of key-value will be copied to kw, but the external variables will not be affected.

5. Named keyword parameters

Sometimes, although we allow keyword parameters to be passed in, we don’t want them to be passed in unscrupulously. At this time, we need to restrict which ones are 可以接收的关键字参数. The main usages are as follows:

#eg1
def get(name, age, *, city, job):
    print(name, age, city, job)
#eg2
def get(name, age, *args, city, job):
    print(name, age, args, city, job)
#eg3 
def get(name, age, *, city='XA', job):
	print(name, age, city, job)
  • Example 1: We can add a * after the positional parameter, and the rest are regarded as named keyword parameters.
  • Example 2: When there are variable parameters in our parameters, there is no need to add * for splitting.
  • Example 3: Named keyword parameters can be omitted.

Notice:

  1. The named keyword must be passed in the parameter name, unless it is a default parameter; for example, in the above example 2, city='xxx' must be used to pass in the named keyword parameter, otherwise an error will be reported.
  2. When * is missing, named keyword arguments are treated as positional arguments.

3. Summary

Through the above description, combined with our practice, we can find that in many cases, the following format can be used to describe a function, which consists of an unlimited number of variable parameters and named keyword parameters.

func(*args, **kwargs)

Guess you like

Origin blog.csdn.net/qq_36306288/article/details/128117566