Python. Argument matching modes. Variable length arguments. Combining different ways of matching arguments. Examples




Argument matching modes. Variable length arguments. Combining different ways of matching arguments. Examples

This topic is a continuation of the topic:

 


Contents


Search other websites:

1. Variable length arguments. Concept

Python language has the ability to pass any number of arguments to a function. This is provided by a special syntax. Any number of arguments can be described in the instructions def function in one of two ways:

  • using a tuple (*);
  • using a dictionary (**).

Accordingly, you can call a function that receives an arbitrary number of parameters.

For a variable number of arguments, the following matching modes are supported in Python:

  • the mode of receiving the function of a variable number of arguments Fn(*sequence) in the form of a tuple;
  • the mode of receiving the function of a variable number of arguments Fn(**name) in the form of a dictionary;
  • mode of passing a variable number of arguments as a tuple: Fn(*args);
  • mode of passing a variable number of arguments in the form of a dictionary: Fn(**args).

 

2. The mode for a function to receive a variable number of arguments Fn(*sequence) as a tuple. Example

The mode of variable number of arguments allows you to call a function with any number of arguments. The arguments that are passed to the function are combined into a collection as a tuple. The order of the arguments in the tuple corresponds to the order in which those arguments were passed to the function.

Example 1. In the example, the Fn() function receives a variable number of arguments as a tuple named sequence. In this case the * symbol is placed in front of the tuple name.

# Arguments. Matching modes.
# Passing an argument to a function - variable number of arguments

# A function that takes a variable number of arguments and displays them on the screen
def Fn(*sequence):
    # the function body contains the sequence value
    print('sequence = ', sequence)
    return

# Function call with 3 arguments
Fn(3, 'abcd', [2, True, 3.888]) # sequence = (3, 'abcd', [2, True, 3.888])

# Function call with 1 argument - dictionary
Fn({1: 'A', 2: 'B', 3: 'C'}) # sequence = ({1: 'A', 2: 'B', 3: 'C'},)

The result of the program

sequence = (3, 'abcd', [2, True, 3.888])
sequence = ({1: 'A', 2: 'B', 3: 'C'},)

 

Example 2. In the example, the Fn() function takes a variable number of arguments. The arguments are passed to the function by the names of the created objects (x, y, z).

# A function that takes a variable number of arguments and displays them on the screen
def Fn(*sequence):
    # the function body displays the value of sequence
    print('Fn.sequence = ', sequence)
    return

# Calling a function with three arguments that have names
x = 2
y = 3
z = 'abcd'
Fn(x, y, z) # Fn.sequence = (2, 3, 'abcd')

The result of the program

Fn.sequence = (2, 3, 'abcd')

 

3. The mode of receiving a variable number of arguments Fn(**name) in the form of a dictionary. Converting arguments that are passed by name to a dictionary. Example

You can pass named arguments to the function. The sequence of these arguments can be converted to a dictionary if the notation ** is used in parentheses in the function declaration (def statement)

def Function(**sequence):
    # ...

The argument that is passed is converted to a separate dictionary element according to the following rule:

  • the name of the argument becomes a key;
  • the value that is assigned to the argument becomes the value of that key.

If Function() is called with two named arguments

Function(X = 5, Y = 'Hello world!')

then a sequence dictionary with the following value will be created in the function body

{ 'X' : 5, 'Y' : 'Hello world!' }

Example. The example demonstrates the code of a program that passes named arguments to a function with a variable number of arguments using the ** syntax.

# Arguments. Matching modes.
# Passing an argument to a function. Variable number of arguments.
# Named arguments are converted to a dictionary

# A function that takes a variable number of arguments and displays them on the screen
def Fn(**dictionary):
    # the function body displays the dictionary value
    print('Fn.dictionary = ', dictionary)
    return

# Calling a function with three named arguments
Fn(x=2, y=5, z=4) # Fn.dictionary = {'x': 2, 'y': 5, 'z': 4}

After starting, the program will give the following result

Fn.dictionary = {'x': 2, 'y': 5, 'z': 4}

As you can see from the result, the call to the Fn() function with the named arguments x, y, z

Fn(x=2, y=5, z=4)

in the function body is converted to a dictionary

{'x': 2, 'y': 5, 'z': 4}

 

4. Variable length arguments. Combining regular arguments with variable arguments * and **. Example

The above modes of passing arguments can be combined to form different ways to call a function.

Example. The example demonstrates the combined passing of arguments in different ways:

  • passing a regular argument combined with passing a variable number of arguments like *;
  • passing a regular argument combined with a dictionary conversion **;
  • passing a normal argument in combination with both * and ** variable number of arguments.

 

# Arguments. Matching modes.
# Passing an argument to a function. Combining matching modes

# 1. A function that combines normal argument passing with a variable of type *
def Fn1(a, *sequence):
    # the function body displays the values of a and sequence
    print('Fn1.a = ', a)
    print('Fn1.sequence = ', sequence)
    return

# Calling the function Fn1()
Fn1(1, 2, 3, 4, 5) # Fn1.a = 1, Fn1.sequence = (2, 3, 4, 5)

# 2. A function that combines normal argument passing with a variable of type **
def Fn2(a, **dic):
    print('Fn2.a = ', a)
    print('Fn2.dic = ', dic)
    return

# Invoke the function Fn2()
Fn2(1, b=2, c=3, d=4, e=5) # Fn.a=1, Fn.dic={'b':2, 'c':3, 'd':4, 'e':5}

# 3. A function that combines normal argument passing with * and ** variables
def Fn3(a, b, *seq, **dic):
    print('Fn3.a = ', a)
    print('Fn3.b = ', b)
    print('Fn3.seq = ', seq)
    print('Fn3.dic = ', dic)
    return

# Invoke the function Fn3()
Fn3(20, 8.55, 9, 4, 7, abc=5, fgh=True) # Fn3.a = 20, Fn3.b = 8.55
# Fn3.seq = (9, 4, 7)
# Fn3.dic = {'abc': 5, 'fgh': True}

Ther result of the program

Fn1.a = 1
Fn1.sequence = (2, 3, 4, 5)
Fn2.a = 1
Fn2.dic = {'b': 2, 'c': 3, 'd': 4, 'e': 5}
Fn3.a = 20
Fn3.b = 8.55
Fn3.seq = (9, 4, 7)
Fn3.dic = {'abc': 5, 'fgh': True}

 

5. The mode of passing a variable number of arguments as a tuple: Fn(*args). Pulling (unpacking) arguments from a tuple. Example

You can pass a tuple with arguments to the function, which will be unpacked into separate arguments in the function. The tuple is pre-formed at the point of the function call. When passing a tuple to a function, a * is placed in front of the tuple name.

To avoid errors, the number of elements of the tuple must be the same as the number of arguments declared in the def statement of the function.

Example. A tuple of three elements is passed to the function Fn(). This tuple is unpacked in the function body. The x, y, z arguments in the function get the corresponding tuple values. In order to demonstrate the possibilities of passing arguments, the function describes an additional default argument v.

# Arguments. Matching modes.
# Passing a tuple to a function. Unpacking a tuple in a function

# A function that takes 4 arguments.
# To demonstrate the possibilities, 1 argument is passed by default.
def Fn(x, y, z, v=25):
    print('Fn.x = ', x)
    print('Fn.y = ', y)
    print('Fn.z = ', z)
    print('Fn.v = ', v)
    return

# Calling a function with a tuple parameter
T = (5, -20.55, 'Hello')
Fn(*T, 7) # Fn.x=5, Fn.y=-20.55, Fn.z=Hello, Fn.v=7

Fn(*T) # Fn.x=5, Fn.y=-20.55, Fn.z=Hello, Fn.v=25

The result of the program

Fn.x = 5
Fn.y = -20.55
Fn.z = Hello
Fn.v = 7
Fn.x = 5
Fn.y = -20.55
Fn.z = Hello
Fn.v = 25

 

6. The mode of passing a variable number of arguments in the form of a dictionary: Fn (** args). Pulling (unpacking) arguments from the dictionary. Example

The function can receive a variable number of arguments using the Fn(**args) dictionary syntax. In this case, a dictionary is passed to the function. In the body of the function, this dictionary is unpacked into separate arguments.

To avoid errors, you need to match the names of the keys in the dictionary with the names of the arguments that the function receives in the def statement.

Example. The passing of the dictionary D to the function Fn() is demonstrated.

# Arguments. Matching modes.
# Passing a dictionary to a function. Unpacking a dictionary into a function

# A function that takes 4 arguments.
def Fn(x, y, z, v):
    print('Fn.x = ', x)
    print('Fn.y = ', y)
    print('Fn.z = ', z)
    print('Fn.v = ', v)
    return

# Calling a function with a dictionary parameter.
# The names of the keys in the dictionary must match the names of the arguments in the function
D = { 'y':7, 'z':5, 'v':3, 'x':4 }
Fn(**D) # Fn.x = 4, Fn.y = 7, Fn.z = 5, Fn.v = 3

# You can also call the function
Fn(**{'v':3, 'y':1, 'x':2, 'z':9})

The result of the program

Fn.x = 4
Fn.y = 7
Fn.z = 5
Fn.v = 3
Fn.x = 2
Fn.y = 1
Fn.z = 9
Fn.v = 3

 

7. Combination of passing arguments as a tuple * with passing as a dictionary **. Example

The passing to a function the tuple (synax Fn(*T)) and the dictionary (syntax Fn (**D)) can be combined. With such passing it is important to observe the correct order of the arguments when calling, as well as the correct indication of the names of the arguments in the dictionary.

Example. Let a function be given that takes 5 arguments. A tuple and a dictionary are passed to the function.

# Arguments. Matching modes.
# Passing a tuple and dictionary to a function.
# Unpacking a tuple and dictionary into a function

# A function that takes 5 arguments.
def Fn(x, y, z, v, w):
    print('Fn.x = ', x)
    print('Fn.y = ', y)
    print('Fn.z = ', z)
    print('Fn.v = ', v)
    print('Fn.w = ', w)
    return

# Function call with tuple and dictionary parameters.
# Tuple is given
T = ( 1, 2)

# Dictionary is given
D = { 'z':3, 'v':4, 'w':5 }

# Call a function passing a tuple and a dictionary
Fn(*T, **D) # Fn.x=1, Fn.y=2, Fn.z=3, Fn.v=4, Fn.w=5

# Fn(**D, *T) - you can not do it this way, error

# Another call
Fn(*(1,2), **{'z':3, 'v':4, 'w':5}) # Fn.x=1, Fn.y=2, Fn.z=3,
# Fn.v=4, Fn.w=5

Ther result of the program

Fn.x = 1
Fn.y = 2
Fn.z = 3
Fn.v = 4
Fn.w = 5
Fn.x = 1
Fn.y = 2
Fn.z = 3
Fn.v = 4
Fn.w = 5

 

8. Examples of combining ordinary arguments, named arguments, and variable number arguments (*, **) in a function call

If a function receives a number of arguments, then arguments can be passed to this function in different ways. These methods combine different modes of passing arguments. The following example demonstrates this.

Example.

# Passing arguments to a function. Combining argument matching modes.

# A function that takes 6 arguments.
def Fn(x, y, z, u, v, w):
    print('x=', x, ', y=', y, ', z=', z, ', u=', u, ', v=', v, ', w=', w)
    return

# 1. Calling a function with ordinary arguments and a tuple
T = ( 10, 20, 30, 40) # A tuple
Fn(5, 3, *T) # x= 5 , y= 3 , z= 10 , u= 20 , v= 30 , w= 40

# Could be so
Fn(2, 4, *(8, -1, 3, 5)) # x= 2 , y= 4 , z= 8 , u= -1 , v= 3 , w= 5

# 2. Calling a function with ordinary arguments and a dictionary
D = { 'w':7, 'u':5, 'v':8, 'z':2 }
Fn(1, 3, **D) # x= 1 , y= 3 , z= 2 , u= 5 , v= 8 , w= 7

# 3. Calling a function with regular arguments, named arguments and a dictionary
Fn(8, y=4, **D) # x= 8 , y= 4 , z= 2 , u= 5 , v= 8 , w= 7

# 4. Calling a function with ordinary arguments and a tuple
T = (7, 8, 9, 10)
Fn(5, 6, *T) # x= 5 , y= 6 , z= 7 , u= 8 , v= 9 , w= 10

# 5. Calling a function with ordinary arguments, tuple and dictionary
T = (5, 4)
D = {'w':1, 'u':3, 'v':2 }
x = 6
Fn(x, *T, **D) # x= 6 , y= 5 , z= 4 , u= 3 , v= 2 , w= 1

The result of the program

x= 5 , y= 3 , z= 10 , u= 20 , v= 30 , w= 40
x= 2 , y= 4 , z= 8 , u= -1 , v= 3 , w= 5
x= 1 , y= 3 , z= 2 , u= 5 , v= 8 , w= 7
x= 8 , y= 4 , z= 2 , u= 5 , v= 8 , w= 7
x= 5 , y= 6 , z= 7 , u= 8 , v= 9 , w= 10
x= 6 , y= 5 , z= 4 , u= 3 , v= 2 , w= 1

 


Related topics