简体   繁体   中英

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

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

I need something like:

PObjectName.update(f'add_{attr_name}_tek'=data)

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)

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'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM