Give function defaults arguments from a dictionary in Python

后端 未结 8 2345
予麋鹿
予麋鹿 2021-01-01 18:26

Let\'s imagine I have a dict :

d = {\'a\': 3, \'b\':4}

I want to create a function f that does the exact same thing than this function :

8条回答
  •  我在风中等你
    2021-01-01 18:48

    Sure... hope this helps

    def funcc(x, **kwargs):
        locals().update(kwargs)
        print(x, a, b, c, d)
    
    kwargs = {'a' : 1, 'b' : 2, 'c':1, 'd': 1}
    x = 1
    
    funcc(x, **kwargs)
    

提交回复
热议问题