Kon Kyri
Kon Kyri

Reputation: 159

Passing arguments of a function as parameter names in other function

Lets assume we have two functions foo and get_idin Python3. I want to call the get_id function inside the foo function while using an argument of the foo function as an argument name in the get_id function. For example:

I call the foo function:

result = foo("Person","name","John")

while the body of the function is:

def foo(arg_1, arg_2, arg_3): res = get_id(arg_1, arg_2 = arg_3) return res

What i want is:

res = get_id("Person", name = "John")

so that the get_id function will be parameterized by the arg_2 argument of the foo function. However, the way i am doing it so far results in error as python interpretes it as a String.Any advice is welcome.

Upvotes: 0

Views: 37

Answers (2)

Corentin Limier
Corentin Limier

Reputation: 5006

This is the way to pass parameter name as string :

def test(a):
    print(a)

test(**{'a': 'hello world'})

I'm sure you can adapt this solution to your problem

Upvotes: 0

Julien C
Julien C

Reputation: 230

I should probably not try to understand why you want to do this :) but here is a solution maybe:

def foo(arg_1, arg_2, arg_3):
    kwargs = {
        'arg_1_name': arg_1,
        arg_2: arg_3,
    }
    return get_id(**kwargs)

just make sure to replace arg_1_name with the actual name of the parameter used in the get_id function

Upvotes: 2

Related Questions