简体   繁体   中英

Passing 'key' dynamically in 'key=value' type parameter

I had this following rough code snippet.

param={'a':1,'b':2}
for k,v in param.items():
     func2(k=v)

func2 is defined as:

func2(**kwargs):
    print kwargs.get('a',None)
    print kwargs.get('b',None)

When func2() is called the variable 'k' is being passed instead of it's value.How to pass k's value to func2(**kwargs) ?

Use ** operator :

for k, v in param.items():
     func2(**{k: v})

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