1

I have a very large amount of variables that I want to return as a dict with their variable names as keys. For example:

return {
    "foo": foo,
    "bar": bar,
    "baz": baz,
}

This is fine for maybe <=10 keys, but after that it becomes a complete mess. In my example I want to return ~100 variables like this.

Is there a one liner that I can use to do something like:

return x(foo, bar, baz)
4

1 回答 1

2

使用locals()

def x(names, values):  
    return dict([ (n, values.get(n)) for n in names ])

x(["foo", "bar", "baz"], locals())
于 2013-05-11T08:11:19.960 回答