59

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
user42876
  • 593
  • 1
  • 4
  • 4
  • 1
    See http://stackoverflow.com/questions/310732/in-django-how-does-one-filter-a-queryset-with-dynamic-field-lookups – S.Lott Dec 03 '08 at 17:56

4 Answers4

68

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?

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 2
    Thanks, that did it. I needed this because I don't have access to the function itself (it's a Django function). So the method itself is already looking for **kwargs, but I need to dynamically create the keyword for the method to parse. – user42876 Dec 03 '08 at 16:36
39

If I understand what you're asking correctly,

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

does what you need.

James Hopkin
  • 13,797
  • 1
  • 42
  • 71
14

You can easily do this by declaring your function like this:

def filter(**kwargs):

your function will now be passed a dictionary called kwargs that contains the keywords and values passed to your function. Note that, syntactically, the word kwargs is meaningless; the ** is what causes the dynamic keyword behavior.

You can also do the reverse. If you are calling a function, and you have a dictionary that corresponds to the arguments, you can do

someFunction(**theDictionary)

There is also the lesser used *foo variant, which causes you to receive an array of arguments. This is similar to normal C vararg arrays.

Benjamin Pollack
  • 27,594
  • 16
  • 81
  • 105
0

Yes, sort of. In your filter method you can declare a wildcard variable that collects all the unknown keyword arguments. Your method might look like this:

def filter(self, **kwargs):
    for key,value in kwargs:
        if key.startswith('min_price__') and key.endswith('__range'):
            currency = key.replace('min_price__', '').replace('__range','')
            rate = self.current_conversion_rates[currency]
            self.setCurrencyRange(value[0]*rate, value[1]*rate)
  • Thanks, but unfortunately I don't have access to the function itself (it's a Django function). So the method itself is already looking for **kwargs, but I need to dynamically create the keyword for the method to parse. – user42876 Dec 03 '08 at 16:28