A
A
Alexander Lebedev2018-02-04 12:43:32
Python
Alexander Lebedev, 2018-02-04 12:43:32

How to pass parameters as a dictionary to a function?

Good morning.
Essence: You need the ability to use the function both independently (just pass parameters to it), and inside a large function (pass a list of dictionaries to a large function so that the working function "eats" the keys as parameters).
Question: Is it possible to pass a list of dictionaries to the function so that it can match its parameters in the dictionary keys? Or, process the dictionary before feeding it to the function, so that it is not passed in the "key:value" format, but in the "key=value" format?
Code example:

subparams = [{
    'param1': 12,
    'param2': 'smth'
},
{
    'param1': 15,
    'param3': 2535
}]

# Большая функция
def main_function(main_param, subparams):

    # Тут что-то происходит

    # Прохожусь по словарям в списке
    for sub in subparams:
        # Передаю словарь в функцию
        subfunction(sub)

    # Что-то возвращается

# Рабочая функция
def subfunction(param1=1, param2='', param3=1000):

    # Тут тоже что-то происходит и возвращается

If there is a more adequate solution or I am missing some important detail - I will be grateful for any advice :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Lebedev, 2018-02-04
@sortarage

While writing the question, I thought from the other end and found a solution :)

d = dict(p1=1, p2=2)
def f2(p1,p2):
    print p1, p2
f2(**d)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question