如何在Python 2.7中将字典作为命名参数传递给函数

时间:2016-12-27 19:17:52

标签: python python-2.7

我有一个包含多个项目的字典。

public string AddToZip(string outfile, string toCompress)
    {
        if (!File.Exists(toCompress)) throw new FileNotFoundException("Could not find the file to compress", toCompress);


        string dir = Path.GetDirectoryName(outfile);
        if(!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }

        // The program that gets this data can't handle files over 
        // 20 MB, so it splits it up into two or more files if it hits the 
        // limit.
        if (File.Exists(outfile))
        {
            FileInfo tooBig = new FileInfo(outfile);
            int converter = 1024;
            float fileSize = tooBig.Length / converter; //bytes to KB
            fileSize = fileSize / converter;  //KB to MB

            int limit = CommonTypes.Helpers.ConfigHelper.GetConfigEntryInt("zipLimit", "19");
            if (fileSize >= limit)
            {
                outfile = MakeNewName(outfile);
            }
        }

        using (ZipFile zf = new ZipFile(outfile))
        {
            zf.AddFile(toCompress,"");
            zf.Save();
        }

        return outfile;          
    }

如何在不编写每个值的情况下将该字典作为命名参数传递?

1 个答案:

答案 0 :(得分:1)

语法为:

FunctionHere(**d)

在此处关于解包参数的部分中记录:https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists