Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top