1. For the caller of a function, it is enough to know how to pass the correct parameters and what value the function will return. The complex logic inside the function is encapsulated and the caller does not need to know.
2. How to calculate x3? You can define another power3 function, but what if you want to calculate x4, x5? It is impossible to define an infinite number of functions.
You may have thought that you can change power(x) to power(x, n) to calculate xn
def power(x, n): s = 1 while n > 0: n = n - 1 s = s * x return s
For this modified power(x, n) function, any nth power can be calculated:
>>> power(5, 2) 25 >>> power(5, 3) 125
The modified power(x, n) function has two parameters: X and N, both of which are location parameters. When calling the function, the two values passed in are assigned to parameters X and N in order of location.
3. Default parameters
The new power(x, n) function definition is OK, but the old calling code failed because we added a parameter, which made the old code unable to call normally due to the lack of a parameter:
>>> power(5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: power() missing 1 required positional argument: 'n'
Python's error message is clear: the calling function power() is missing a positional parameter n.
At this time, the default parameters come in handy. Since we often calculate x2, we can set the default value of the second parameter n to 2:
def power(x, n=2): s = 1 while n > 0: n = n - 1 s = s * x return s
In this way, when we call power(5), it is equivalent to calling power(5, 2):
>>> power(5) 25 >>> power(5, 2) 25
For other cases where n > 2, n must be explicitly passed in, such as power(5, 3).
As can be seen from the above example, the default parameter can simplify the function call. There are several points to note when setting default parameters:
First, the required parameter comes first and the default parameter comes last, otherwise the Python interpreter will report an error (think about why the default parameter cannot be placed in front of the required parameter);
The second is how to set default parameters.
When a function has multiple parameters, put the parameters with large changes in front and the parameters with small changes in back. Parameters with small changes can be used as default parameters.
What are the benefits of using default parameters? The biggest advantage is that it can reduce the difficulty of calling functions.
4. Variable parameters
Compared with defining a list or tuple parameter, defining a variable parameter only adds an * sign in front of the parameter. Inside the function, the parameter numbers receives a tuple, so the function code is completely unchanged. However, when calling this function, you can pass in any parameter, including 0 parameters:
def calc(*numbers): sum = 0 for n in numbers: sum = sum + n * n return sum
>>> calc(1, 2) 5 >>> calc() 0
If there is already a list or tuple, what should I do to call a variable parameter? This can be done:
>>> nums = [1, 2, 3] >>> calc(nums[0], nums[1], nums[2]) 14
Of course, this way of writing is feasible. The problem is that it is too cumbersome. Therefore, Python allows you to add an * sign in front of the list or tuple to turn the elements of the list or tuple into variable parameters and pass them in:
>>> nums = [1, 2, 3] >>> calc(*nums) 14
*nums means to pass all elements of the list as variable parameters. This kind of writing is quite useful and common.
5. Keyword parameter (compared with variable parameter, it is * * in the form of Dictionary)
It allows you to pass in 0 or any parameters. These variable parameters are automatically assembled into a tuple when the function is called. Keyword parameters allow you to pass in 0 or any parameter with parameter name. These keyword parameters are automatically assembled into a dict inside the function
In addition to the required parameters name and age, the function person also accepts the keyword parameter kw. When calling this function, you can only pass in the required parameters:
You can also pass in any number of keyword parameters:
>>> person('Bob', 35, city='Beijing') name: Bob age: 35 other: {'city': 'Beijing'} >>> person('Adam', 45, gender='M', job='Engineer') name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}
What is the use of keyword parameters? It can extend the function. For example, in the person function, we guarantee to receive the name and age parameters, but if the caller is willing to provide more parameters, we can also receive them. Imagine that you are doing a user registration function. Except that the user name and age are required, others are optional. Using keyword parameters to define this function can meet the needs of registration.
6. Name keyword parameters
If you want to limit the name of keyword parameters, you can use named keyword parameters. For example, only city and job are accepted as keyword parameters. The functions defined in this way are as follows:
def person(name, age, *, city, job): print(name, age, city, job)
Unlike the keyword parameter * * kw, the named keyword parameter requires a special separator *, and the parameters after * are regarded as named keyword parameters.
The calling method is as follows:
>>> person('Jack', 24, city='Beijing', job='Engineer') Jack 24 Beijing Engineer
If there is already a variable parameter in the function definition, the following named keyword parameters do not need a special separator *:
def person(name, age, *args, city, job): print(name, age, args, city, job)