• 5 Types of Arguments in Python Function Definition
  • Indhumathy Chelliah
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Zhengjian – L
  • Proofread by: z0gSh1u, JalanJiang

Five arguments to define Python functions

Python defines five arguments to a function:

  1. The default parameters
  2. Keyword parameter
  3. Positional arguments
  4. Arbitrary position parameter
  5. Any keyword argument

Python function definitions:

The keyword def introduces a function definition. It must be followed by the function name and a parenthesized list of formal arguments. The statements that make up the function body begin on the next line and must be indented.

Formal parameters need to be mentioned in defined functions. The actual arguments are passed when the function is called.

We can define functions with various parameters.

1. Default parameters:

  • Default parameters are the values provided when the function is defined.
  • Assignment symbol=Used to declare default values for parameters.
  • When a function is called, the parameter defaults to mutable items.
  • If the value of the default argument is provided when the function is called, it replaces the default value.
  • A function can have any number of default arguments.
  • Default parameters must come after non-default parameters.

Ex. :

B,c In the following example, parameters b,c specify default values.

def add(a,b=5,c=10) :
    return (a+b+c)
Copy the code

This function can be called in three ways

1. Give only required parameters

print(add(3))
#Output:18
Copy the code

2. Assign an optional parameter 3 to A and 4 to b.

print(add(3.4))
#Output:14
Copy the code

3. Give all parameters

print(add(2.3.4))
#Output:9
Copy the code

Note: Default values are evaluated only once during the definition at the function definition. As a result, mutable objects such as lists and dictionaries will be slightly different when used as defaults.

2. Keyword parameter:

You can also call a function with a keyword argument of the form kwarg=value.

When a function is called, the parameter values do not need to be in the same order as the parameters in the function definition. This can be done with keyword arguments. However, all keyword arguments must correspond one to one to the parameters in the function definition.

Ex. :

def add(a,b=5,c=10) :
    return (a+b+c)
Copy the code

The keyword argument is provided when the function add is called

  1. All arguments are keyword arguments, so there is no need for a fixed order.
print (add(b=10,c=15,a=20))
#Output:45
Copy the code
  1. When a function is called, only necessary arguments are given as keyword arguments, and optional default arguments are skipped.
print (add(a=10))
#Output:25
Copy the code

3. Position parameters

When calling a function, the order in which the parameters are passed needs to correspond to the order in which the formal parameters are passed. These are called positional parameters.

Positional arguments can only be followed by keyword arguments.

Ex. :

def add(a,b,c) :
    return (a+b+c)
Copy the code

The above function can be called in two ways:

  1. When a function is called, arguments are positional arguments. The value passed by the parameter is passed to the corresponding parameter by position.10Assigned to a.20Assigned to b30Assigned toc.
print (add(10.20.30))
#Output:60
Copy the code
  1. When you mix positional and keyword arguments, the keyword argument always comes after the positional argument.
print (add(10,c=30,b=20))
#Output:60
Copy the code

Comparison of default parameters, location parameters, and keyword parameters:


Key points:

1. The default parameter must come after the non-default parameter

def add(a=5,b,c) :
    return (a+b+c)

#Output:SyntaxError: non-default argument follows default argument
Copy the code

2. The keyword parameter must come after the position parameter

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

print (add(a=10.3.4))
#Output:SyntaxError: positional argument follows keyword argument
Copy the code

3. All keyword parameters passed must have corresponding parameters, and the order is not important.

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

print (add(a=10,b1=5,c=12))
#Output:TypeError: add() got an unexpected keyword argument 'b1'
Copy the code

4. The parameter can be assigned only once

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

print (add(a=10,b=5,b=10,c=12))
#Output:SyntaxError: keyword argument repeated
Copy the code

5. Default parameters are optional

Example 1: Give only necessary parameters

def add(a,b=5,c=10) :
    return (a+b+c)

print (add(2))
#Output:17
Copy the code

Example 2: Give all parameters (necessary and optional)

def add(a,b=5,c=10) :
    return (a+b+c)

print (add(2.3.4))
#Output:9
Copy the code

Variable length parameter

Variable-length parameters are also called arbitrary parameters. If we don’t know the number of arguments to the function in advance, we can use any argument.

Two arbitrary parameters

  1. Arbitrary position parameter
  2. Any keyword argument

4. Arbitrary position parameters:

For any positional argument, the argument defined by the function is preceded by an asterisk (*), which can contain non-keyword variable-length arguments. These parameters will be contained in a tuple. Zero or more ordinary arguments may appear before a variable number of arguments.

def add(*b) :
    result=0
    for i in b:
         result=result+i
    return result

print (add(1.2.3.4.5))
#Output:15

print (add(10.20))
#Output:30
Copy the code

5. Arbitrary keyword arguments:

For any keyword argument, the function defines an argument preceded by a double star (*), which can contain non-keyword variable-length arguments.

Ex. :

def fn(**a) :
    for i in a.items():
        print (i)
fn(numbers=5,colors="blue",fruits="apple")
'''
Output:
('numbers', 5)
('colors', 'blue')
('fruits', 'apple')
'''
Copy the code

Special parameters:

According to the Python manual

By default, function arguments can be passed as positional or explicit keyword arguments. To ensure readability and runtime efficiency, it makes sense to limit the way arguments are passed so that developers can look at the function definition to determine whether argument items are passed by location only, location and keyword, or keyword only.

The function definition might look something like this:

Here/and * are optional. If used, these symbols indicate what parameters can be used to pass parameter values to a function: position only, position or keyword, and keyword only.

  1. Location or keyword arguments
  2. Positional parameters only
  3. Keyword arguments only

1. Position or keyword parameters

If/and * are not used in the function definition, arguments can be passed to the function by position or by keyword.

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

print (add(3.4.5))
#Output:12

print (add(3,c=1,b=2))
#Output:6
Copy the code

2. Position parameters only

In the function defined, position-only arguments are preceded by/(forward slash). This/is used to logically separate position-only parameters from other parameters. The parameter after/can be position or keyword or keyword only.

def add(a,b,/,c,d) :
    return a+b+c+d

print (add(3.4.5.6))
#Output:18

print (add(3.4,c=1,d=2))
#Output:10
Copy the code

Specifying keyword arguments as position-only arguments causes TypeError.

def add(a,b,/,c,d) :
    return a+b+c+d

print (add(3,b=4,c=1,d=2))
#Output:TypeError: add() got some positional-only arguments passed as keyword arguments: 'b'
Copy the code

3. Only keyword parameters are allowed

To mark a parameter as keyword-only, place an * before the first keyword-only parameter in the parameter list.

def add(a,b,*,c,d) :
    return a+b+c+d

print (add(3.4,c=1,d=2))
#Output:10
Copy the code

If the positional argument is specified as a keyword-only argument, TypeError is caused.

def add(a,b,*,c,d) :
    return a+b+c+d

print (add(3.4.1,d=2))
#Output:TypeError: add() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Copy the code

A call rule that has three arguments in the same function

In the example below, function Add has all three arguments

A,b – Position parameters only C – Position or keyword parameters d – Keyword parameters only

def add(a,b,/,c,*,d) :
    return a+b+c+d

print (add(3.4.1,d=2))
#Output:10
Copy the code

Matters needing attention:

  1. Use position-only when you want the name of a parameter not to be available to the user. This is useful when parameter names have no practical meaning.
  2. If you want to specify the parameter data for a function call, use only positions.
  3. Keyword only is used when the name makes sense and the defined function is made easier to understand by the name.
  4. Use only keywords when you want to avoid user dependence on the location of passed parameters.

Source (Python Manual) :

Define a function

The default parameters

Keyword parameter

Special parameters

Arbitrary argument list

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.