Dynamic Keyword Arguments in Python?

Does python have the ability to create dynamic keywords?

For example:

qset.filter(min_price__usd__range=(min_price, max_price))

I want to be able to change the usd part based on a selected currency.


Solution 1:

Yes, It does. Use **kwargs in a function definition.

Example:

def f(**kwargs):
    print kwargs.keys()


f(a=2, b="b")     # -> ['a', 'b']
f(**{'d'+'e': 1}) # -> ['de']

But why do you need that?

Solution 2:

If I understand what you're asking correctly,

qset.filter(**{
    'min_price_' + selected_currency + '_range' :
    (min_price, max_price)})

does what you need.