0

Using a dynamic variable name, as an argument for a function/method:

I need something like:

PObjectName.update(f'add_{attr_name}_tek'=data)
1
  • 2
    Don't use dynamic variables Commented Apr 17, 2019 at 15:11

2 Answers 2

2

When you want to dynamically build a set of named arguments for a function call, you can use a dictionary and pass it using the ** operator to expand it in the function call.

concretely, you can do this way:

kwargs = {f'add_{attr_name}_tek': data}

PObjectName.update(**kwargs)
Sign up to request clarification or add additional context in comments.

Comments

1

It is possible with unpacking a dict of keyword arguments:

def foo(add_attr_name_tek):
    print(add_attr_name_tek)

attr_name = 'attr_name'

foo(**{f'add_{attr_name}_tek': 'value'})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.