Python. Argument matching modes. Classification. Matching by position. Matching by name. Default arguments. Examples

Argument matching modes. Classification. Matching by position. Matching by name. Default arguments. Examples


Contents


Search other websites:




1. Argument matching modes in Python. Classification

As you know, in Python, passing an argument to a calling function is done through an assignment operation. When a function is called, the object that is passed is assigned to the argument (parameter) of the function, which is described in parentheses in the def statement

def Fn(a):
    # Using the argument (parameter) a in the function Fn
    # ...
    return

# Calling the function Fn and passing it object X
X = 28
Fn(X) # The function Fn () implicitly performs the assignment a = X

Python, like other programming languages, supports a variety of argument matching modes when calling functions. Below is a list of these modes:

  • position matching mode Fn(value). In this mode, the values that are passed to the function are assigned to names in the function in order from left to right (see p. 2);
  • name matching mode Fn(name = value);
  • default argument passing mode;
  • passing/receiving mode of a variable number of arguments;
  • generalized ways to call a function.

 

2. Matching by position: Fn(value). Example

This is the classic way of passing an argument. In this mode, the values that are passed are assigned to names in the function in order from left to right.

Example.

# Arguments. Matching modes
# Passing an argument to a function - matching by position

# A function that takes 4 arguments
def Fn(a, b, c, d):
    # the function body displays the values of the arguments with names
    print('Fn.a = ', a)
    print('Fn.b = ', b)
    print('Fn.c = ', c)
    print('Fn.d = ', d)
    return

# Passing objects to the Fn() function
Fn(1, 2, 3, 4) # Fn.a=1, Fn.b=2, Fn.c=3, Fn.d=4

After running for execution, the program will give the following result

Fn.a = 1
Fn.b = 2
Fn.c = 3
Fn.d = 4

As you can see from the result, when calling the function Fn()

Fn(1, 2, 3, 4)

number (object) 1 is assigned to parameter a in the body of the function. Accordingly, numbers 2, 3, 4 are assigned to parameters b, c, d in the body of the function.

 

3. Matching by name: Fn(name = value). Example

Name matching is a mode in which, when a function is called, the arguments (objects) contain names that are described by parameters in the function body (in the def statement). The parameter name in the def statement is assigned a value that is assigned to the same name in the function call.

Example.

# Arguments. Matching modes.
# Passing an argument to a function - matching by name

# A function that takes 4 arguments
def Fn(a, b, c, d):
    # the function body displays the values of the arguments with names
    print('Fn.a = ', a)
    print('Fn.b = ', b)
    print('Fn.c = ', c)
    print('Fn.d = ', d)
    return

# Passing objects to the Fn() function - specifying specific names
Fn(d=4, a=1, c=3, b=2) # Fn.a=1, Fn.b=2, Fn.c=3, Fn.d=4

The result of the program

Fn.a = 1
Fn.b = 2
Fn.c = 3
Fn.d = 4

If, when calling the function Fn(), specify a non-existent parameter, the interpreter will generate an error. For example, if in the function call the fragment c=3 is replaced by f=3

Fn(d=4, a=1, f=3, b=2)

then the interpreter will give the following error

TypeError: Fn() got an unexpected keyword argument 'f'

 

4. The default argument passing mode: def Fn(name = value). Example

In this mode, when declaring a function (in a def statement), parameters can be set to their default values. This means that if a parameter with a default value does not receive a value in the calling code, then this parameter will be set to this default value.

For example, if a function is declared that receives a default value as shown below

def Fn(a=5):
    # function body
    # ...
    return

then this function can be called in two ways

...
Fn()
Fn(value)
...

where value is some object (number, string, list, etc.) created earlier in the program.

In the first case, a function with no parameters is called. This means that in the body of the function, the parameter a takes the value 5 (by default). In the second case, the function is called with one parameter value. This means that in the body of the function, the parameter a receives the passed value.

Example. In the example, the function receives 4 parameters. Parameters c and d get default values, 10 and 100, respectively.

# Arguments. Matching modes.
# Passing an argument to a function - specifying default values

# A function that takes 4 parameters.
# Parameters c, d get default values, respectively 10 and 100
def Fn(a, b, c=10, d=100):
    # the function body displays the parameter values with names
    print('Fn.a = ', a)
    print('Fn.b = ', b)
    print('Fn.c = ', c)
    print('Fn.d = ', d)
    return

# Passing objects to the Fn() function - the default values are set
Fn(1,2,3,4) # Fn.a=1, Fn.b=2, Fn.c=3, Fn.d=4
Fn(1,2,3)   # Fn.a=1, Fn.b=2, Fn.c=3, Fn.d=100
Fn(1,2)     # Fn.a=1, Fn.b=2, Fn.c=10, Fn.d=100

As you can see from the above code, the Fn() function can be called with two, three, and four arguments.

The result of the program

Fn.a = 1
Fn.b = 2
Fn.c = 3
Fn.d = 4
Fn.a = 1
Fn.b = 2
Fn.c = 3
Fn.d = 100
Fn.a = 1
Fn.b = 2
Fn.c = 10
Fn.d = 100

 

5. The default argument passing mode. Default order of arguments in def statement

If defaults are assigned to the argument names in the function description in the def statement, then a certain order must be observed. This order is determined by the following rule:

  • the first are the names of the arguments that do not receive a default value. And after that, the names of the arguments are determined, which receive default values.

For example. Let the function Fn() be given, which takes three arguments a, b, c. If, when describing a function, for the arguments a, b, set the default values as follows

def Fn(a=1, b=2, c): # this is an erroneous code
    # ...
    # ...
    return

then when starting the program for execution, the Python interpreter will generate a syntax error with the following message

non default argument follows default argument

So, the default arguments should be specified last in the list of function arguments in the def statement.

To improve the situation, you can write the following code for declaring the function Fn()

def Fn(c, a=1, b=2): # this is the correct code
    # the body of the function Fn ()
    # ...

In the above declaration of the Fn() function, the default argument names a, b follow the non-default argument c.

 

6. In what order does the interpreter match the arguments in the function argument list?

If a function is declared (a def statement) containing arguments, then the interpreter follows a strictly defined order of using argument matching. This order is as follows:

  1. Matching unnamed arguments by position (see p. 2).
  2. Matching arguments by name (see p. 3).
  3. Matching optional variable-length unnamed arguments using the *sequence syntax of tuple (see section 6). See here for more details on matching unnamed arguments.
  4. Matching optional variable-length named arguments with the conversion to dictionary **name. For more information on converting variable length arguments to a dictionary, see here.
  5. Matching of default values (see item 4).

 


Related topics