pass **kwargs argument to another function with **kwargs

后端 未结 5 1883
青春惊慌失措
青春惊慌失措 2020-11-30 19:06

I do not understand the following example, lets say I have these functions:

# python likes
def save(filename, data, **kwargs):
    fo = openX(filename, \"w\"         


        
5条回答
  •  误落风尘
    2020-11-30 19:23

    In the second example you provide 3 arguments: filename, mode and a dictionary (kwargs). But Python expects: 2 formal arguments plus keyword arguments.

    By prefixing the dictionary by '**' you unpack the dictionary kwargs to keywords arguments.

    A dictionary (type dict) is a single variable containing key-value pairs.

    "Keyword arguments" are key-value method-parameters.

    Any dictionary can by unpacked to keyword arguments by prefixing it with ** during function call.

提交回复
热议问题