Python’s function definitions are very simple and flexible. In addition to the normally defined mandatory parameters, default, variable, and keyword parameters can be used, allowing functions to define interfaces that can not only handle complex parameters, but also simplify the caller’s code.

Positional arguments

Let’s write a function that computes x squared:

def power(x): return x * xCopy the code

The argument x here is a positional argument.

When we call the power function, we must pass in one and only argument x:

>>> power(5)25Copy the code

So if I want to compute x to the n, what do I do?

You’re smart enough to think of that. Define two parameters as follows:

def power(x, n): s = 1 while n > 0: n = n - 1 s = s * x return sCopy the code

The default parameters

The new power(x, n) function definition worked fine, but the old calling code failed because we added an argument, causing the old code to fail because it lacked one argument. This is where the default parameters come in handy. Since we always compute x squared, it is perfectly acceptable to set the default value of the second parameter n to 2.


As you can see from the example above, default arguments simplify function calls. When setting the default parameters, note the following:

The Python interpreter will report an error if the default argument is not placed before the mandatory argument.

The second is how to set the default parameters.

When a function has more than one argument, place the argument that changes a lot first and the argument that changes a little later. Parameters that change little can be used as default parameters.

What are the benefits of using default parameters? The biggest benefit is that it reduces the difficulty of calling the function.

One thing to remember when defining default parameters is that they must point to immutable objects!

Why design immutable objects like STR and None? Because once an immutable object is created, the data inside the object cannot be modified, errors caused by modifying the data are reduced. In addition, since objects are invariant, simultaneous reading of objects in a multitasking environment does not require locking, and simultaneous reading has no problem at all. When we write a program, if we can design an immutable object, we should design it as immutable as possible.

Variable parameter

You can also define mutable parameters in Python functions. As the name implies, a mutable parameter is a variable number of arguments passed in. It can be 1, 2, or any number, or zero.

Let’s take a mathematical problem as an example. Given a set of numbers A, B, C… , please calculate A2 + B2 + C2 +…… .


Defining a variable parameter simply precedes the parameter with an asterisk (*), as opposed to defining a list or tuple. Inside the function, the argument numbers receives a tuple, so the code is completely unchanged. However, this function can be called with any number of arguments, including zero.

What if you already have a list or tuple and want to call a mutable parameter? *nums means that all elements of the nums list are passed in as variables. This is quite useful and very common.

Keyword parameter

Keyword arguments allow you to pass in zero or any arguments with parameter names, which are automatically assembled as a dict inside the function.

def person(name, age, **kw): print('name:', name, 'age:', age, 'other:', kw)Copy the code

The person function accepts the keyword argument kw in addition to the mandatory arguments name and age. When you call this function, you can pass in only mandatory arguments.

person('Michael', 30)Copy the code

You can also pass in any number of keyword arguments:

person('Bob', 35, city='Beijing')Copy the code
person('Adam', 45, gender='M', job='Engineer')Copy the code

What is the use of keyword arguments? It extends the functionality of a function. For example, in the person function, we are guaranteed to receive the name and age arguments, but we can also receive more arguments if the caller is willing to provide them. Imagine that you are creating a user registration function. All of the functions are optional except username and age. Defining this function with the keyword parameter will satisfy the registration requirement.

Name keyword arguments

For keyword arguments, the caller of a function can pass in any unrestricted keyword argument. As to what is passed in, you need to pass the KW check inside the function. If you want to limit the names of keyword arguments, you can use named keyword arguments, for example, accepting only city and job as keyword arguments. The function defined in this way is as follows:

def person(name, age, *, city, job): print(name, age, city, job)Copy the code

Unlike the keyword argument **kw, the named keyword argument requires a special delimiter *, and the argument after * is treated as the named keyword argument.

Call as follows:

person('Jack', 24, city='Beijing', job='Engineer')Copy the code

If the function definition already has a mutable argument, the following named keyword argument does not need a special delimiter * :

def person(name, age, *args, city, job): print(name, age, args, city, job)Copy the code

Parameter combination

To define functions in Python, you can use mandatory, default, variable, keyword, and named keyword arguments, all of which can be used in combination. Note, however, that the parameters must be defined in the order required, default, variable, named keyword, and keyword arguments.

For example, define a function that takes several of the above parameters: