Python: Passing parameters by name along with kwargs

后端 未结 3 1806
傲寒
傲寒 2020-12-28 13:38

In python we can do this:

def myFun1(one = \'1\', two = \'2\'):
    ...

Then we can call the function and pass the arguments by their name:

3条回答
  •  暖寄归人
    2020-12-28 14:01

    You can add your named arguments along with kwargs. If the keys are available in the calling function It will taken to your named argument otherwise it will be taken by the kwargs dictionary.

    def add(a=1, b=2,**c):
        res = a+b
        for items in c:
            res = res + c[items]
        print(res)
    
    add(2,3)
    

    5

    add(b=4, a =3)
    

    7

    add(a =1,b=2,c=3,d=4)
    

    10

提交回复
热议问题