Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top