Suppose I have a function with this signature:

def foo(a=5, b=10, c=15)

Suppose I also have a variable, x, whose value is 'a', 'b', or 'c'. Effectively, what I want to do is this:

foo(**{x: 7})

But it seems like there may be a more straightforward way of doing this. Is my suspicion correct?

有帮助吗?

解决方案

Whenever you want to do something dynamic that's normally static, Python generally forces you to be explicit about it (because explicit is better than implicit).

So, in this case, yes, foo(**{x: 7}) is probably the most pythonic way to write it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top