Using named arguments as variables
<p>I am using django but I think this question primarily belongs to Python itself.</p>

<p>I have something like:</p>

<pre><code>def get(self, request, arg, *args, **kwargs):

    if kwargs['m0'] == 'death':
      if kwargs['m1'] == 'year':
         result = Artists.objects.filter(death_year=arg)
      elif kwargs['m1'] == 'month':
         result = Artists.objects.filter(death_month=arg)
      elif kwargs['m1'] == 'day':
         result = Artists.objects.filter(death_day=arg)
    elif kwargs['m0'] == 'birth':
      if kwargs['m1'] == 'year':
         result = Artists.objects.filter(birth_year=arg)
      elif kwargs['m1'] == 'month':
         result = Artists.objects.filter(birth_month=arg)
      elif kwargs['m1'] == 'day':
         result = Artists.objects.filter(birthh_day=arg)
</code></pre>

<p>Where <code>death_year</code> is a named argument that is a  field in my model <code>Artists</code> representing a column in my database. The variables 'm0' and <code>m1</code> are passed from the <code>urlconf</code> to my get function (it is actually a <code>get</code> method in my view class).
Can I control the name value of the variable <code>death_year</code> without using an if else if chain (i.e. make it <code>death_month</code> or <code>birth_year</code>)? Since I have many choices, I will have to use a ridiculously very long conditional chain that leads to this same line but with just a different named argument.</p>

<p>I strongly doubt that you should understand this whole problem to answer the question. The original question is simple: Can I use a named argument and its corresponding value as variables in Python?</p>

<p>The basic and incredibly powerful idea in Python is that you can pass a list of positional arguments without writing <code>args[0], args[1],...</code> and/or a dict of keyword arguments without writing <code>foo=kwargs["foo"], bar=kwargs["bar"],...</code> by using <code>*</code> and <code>**</code> as in <code>func( *args, **kwargs)</code></p>

<p>A function can likewise be written to accept a list of positional args of arbitrary length, and any set of unmatched keyword arguments: <code>def func( a, b, c=0, *args, **kwargs)</code>. (<code>args</code> and <code>kwargs</code> are not reserved words, just conventional variable names)</p>

<p>This example probably shows most of it in operation:</p>

<pre><code>&gt;&gt;&gt; def foo( a, b=0, c=0, *args, **kwargs):
...   print( "a,b,c: ", a, b, c)
...   print( args)
...   print( kwargs)
... 
&gt;&gt;&gt; foo( *[1,2,3,4], **{"baz":42} )
a,b,c:  1 2 3
(4,)
{'baz': 42}

&gt;&gt;&gt; foo( 1, b=2,  **{"baz":42, "c":3} )
a,b,c:  1 2 3
()
{'baz': 42}

&gt;&gt;&gt; foo( **{"baz":42, "c":3, "a":1, "zork":"?" } )
a,b,c:  1 0 3
()
{'zork': '?', 'baz': 42}
</code></pre>

<p>kwargs is a dict so if you want, you can use <code>kwargs.keys()</code> and all the other <code>dict</code> methods to introspect what the user actually passed and validate, parse, process etc. Django frequently uses <code>kwargs.pop()</code> to allow the fetching of arguments for the current function, while leaving other named arguments to propagate down a possibly deep set of nested function invocations until a routine near the bottom finds an optional keyword argument of its own. </p>

python django