Python list of functions to call with arguments

The simpliest way to wrap a function

    func(*args, **kwargs)

... is to manually write a wrapper that would call func() inside itself:

    def wrapper(*args, **kwargs):
        # do something before
        try:
            return func(*a, **kwargs)
        finally:
            # do something after

In Python function is an object, so you can pass it's name as an argument of another function and return it. You can also write a wrapper generator for any function anyFunc():

    def wrapperGenerator(anyFunc, *args, **kwargs):
        def wrapper(*args, **kwargs):
            try:
                # do something before
                return anyFunc(*args, **kwargs)
            finally:
                #do something after
        return wrapper

Please also note that in Python when you don't know or don't want to name all the arguments of a function, you can refer to a tuple of arguments, which is denoted by its name, preceded by an asterisk in the parentheses after the function name:

    *args

For example you can define a function that would take any number of arguments:

    def testFunc(*args):
        print args    # prints the tuple of arguments

Python provides for even further manipulation on function arguments. You can allow a function to take keyword arguments. Within the function body the keyword arguments are held in a dictionary. In the parentheses after the function name this dictionary is denoted by two asterisks followed by the name of the dictionary:

    **kwargs

A similar example that prints the keyword arguments dictionary:

    def testFunc(**kwargs):
        print kwargs    # prints the dictionary of keyword arguments

In Python, you can unpack list, tuple, dict (dictionary) and pass its elements to function as arguments by adding * to list or tuple and ** to dictionary when calling function.

This article describes the following contents.

  • Unpack list and tuple with *
    • With default arguments
    • With variable-length arguments
  • Unpack dict (dictionary) with **
    • With default arguments
    • With variable-length arguments

Note that if list, tuple, or dict is specified as an argument without * or **, it is not be unpacked and passed to the function as is.

See the following articles for basic usage of functions in Python, default arguments, and variable-length arguments with * and ** when defining functions.

  • Define and call functions in Python (def, return)
  • Default arguments in Python
  • Variable-length arguments (args, *kwargs) in Python

Unpack list and tuple with *

When specifying a list or tuple with * as an argument, it is unpacked, and each element is passed to each argument.

def func(arg1, arg2, arg3):
    print('arg1 =', arg1)
    print('arg2 =', arg2)
    print('arg3 =', arg3)

l = ['one', 'two', 'three']

func(*l)
# arg1 = one
# arg2 = two
# arg3 = three

func(*['one', 'two', 'three'])
# arg1 = one
# arg2 = two
# arg3 = three

t = ('one', 'two', 'three')

func(*t)
# arg1 = one
# arg2 = two
# arg3 = three

func(*('one', 'two', 'three'))
# arg1 = one
# arg2 = two
# arg3 = three

Lists are used in the following sample code, but the same applies to tuples.

If the number of elements does not match the number of arguments, TypeError raises.

# func(*['one', 'two'])
# TypeError: func() missing 1 required positional argument: 'arg3'

# func(*['one', 'two', 'three', 'four'])
# TypeError: func() takes 3 positional arguments but 4 were given

With default arguments

If the function has default arguments, the default arguments are used if the number of elements is insufficient. If there are many elements, TypeError raises.

def func_default(arg1=1, arg2=2, arg3=3):
    print('arg1 =', arg1)
    print('arg2 =', arg2)
    print('arg3 =', arg3)

func_default(*['one', 'two'])
# arg1 = one
# arg2 = two
# arg3 = 3

func_default(*['one'])
# arg1 = one
# arg2 = 2
# arg3 = 3

# func_default(*['one', 'two', 'three', 'four'])
# TypeError: func_default() takes from 0 to 3 positional arguments but 4 were given

With variable-length arguments

If the function has a variable-length argument (*args), all elements after the positional argument are passed to the variable-length argument.

def func_args(arg1, *args):
    print('arg1 =', arg1)
    print('args =', args)

func_args(*['one', 'two'])
# arg1 = one
# args = ('two',)

func_args(*['one', 'two', 'three'])
# arg1 = one
# args = ('two', 'three')

func_args(*['one', 'two', 'three', 'four'])
# arg1 = one
# args = ('two', 'three', 'four')

Unpack dict (dictionary) with **

By adding ** to dict (dictionary) and specifying it as an argument, its keys and values are treated as names and values of arguments. Each element is passed as keyword arguments.

def func(arg1, arg2, arg3):
    print('arg1 =', arg1)
    print('arg2 =', arg2)
    print('arg3 =', arg3)

d = {'arg1': 'one', 'arg2': 'two', 'arg3': 'three'}

func(**d)
# arg1 = one
# arg2 = two
# arg3 = three

func(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three'})
# arg1 = one
# arg2 = two
# arg3 = three

If there are not enough keys or keys that do not match the argument names, TypeError raises.

# func(**{'arg1': 'one', 'arg2': 'two'})
# TypeError: func() missing 1 required positional argument: 'arg3'

# func(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three', 'arg4': 'four'})
# TypeError: func() got an unexpected keyword argument 'arg4'

With default arguments

If the function has default arguments, only the value of the argument name matching the dictionary key is updated.

If a key does not match the argument name, TypeError is raised.

def func_default(arg1=1, arg2=2, arg3=3):
    print('arg1 =', arg1)
    print('arg2 =', arg2)
    print('arg3 =', arg3)

func_default(**{'arg1': 'one'})
# arg1 = one
# arg2 = 2
# arg3 = 3

func_default(**{'arg2': 'two', 'arg3': 'three'})
# arg1 = 1
# arg2 = two
# arg3 = three

# func_default(**{'arg1': 'one', 'arg4': 'four'})
# TypeError: func_default() got an unexpected keyword argument 'arg4'

With variable-length arguments

If the function has a variable-length argument (**kwargs), all elements with keys that do not match the argument name are passed to the variable-length argument.

def func_kwargs(arg1, **kwargs):
    print('arg1 =', arg1)
    print('kwargs =', kwargs)

func_kwargs(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three'})
# arg1 = one
# kwargs = {'arg2': 'two', 'arg3': 'three'}

func_kwargs(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three', 'arg4': 'four'})
# arg1 = one
# kwargs = {'arg2': 'two', 'arg3': 'three', 'arg4': 'four'}

func_kwargs(**{'arg1': 'one', 'arg3': 'three'})
# arg1 = one
# kwargs = {'arg3': 'three'}

Are lists of function calls allowed in Python?

In Python, you can use a list function which creates a collection that can be manipulated for your analysis. This collection of data is called a list object.

Can Python function take list as argument?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

How do I call a function with a parameter in Python?

How to Define and Call a Basic Function in Python.
Type the function name..
The function name has to be followed by parentheses. If there are any required arguments, they have to be passed in the parentheses. If the function doesn't take in any arguments, you still need the parentheses..