kwargs keyword evaluation

T

tzellman

Ok, so here is my situation:

Let's assume I have a function that makes good use of the kwargs
parameter. It requires that there is a certain "format" for the kwargs
keywords. (I am using Django, btw). The format is like such:
"SOMEVAL__exact", etc., where SOMEVAL is some value that it parses from
the keyword.

Now, I want to call this function, specifying the kwargs. The problem
is that I want to dynamically set the kwargs. For example, I don't want
to hard code in the name like such:
function_call(name__exact="Tom")

I want to generate the keyword on the fly. So, I want to do this:
(assume someVar == 'name' for now)
keyword = someVar + "__exact"

The problem:
I need to know how to get it to use the VALUE of the keyword for the
keyword.


My thoughts:
(1) Maybe I can override the kwargs parameter by doing such:
function_call(kwargs={keyword:"Tom"})
*loud buzzer* Nope. (as expected)

(2) Maybe I can just pass it in:
function_call(keyword="Tom")
Nope! (as expected, it tries to use 'keyword' as the keyword)

What can I do?!?!?!?
I really don't want to have to hardcode a BUNCH of if statements.

Thanks in advance!
 
F

Fredrik Lundh

Let's assume I have a function that makes good use of the kwargs
parameter. It requires that there is a certain "format" for the kwargs
keywords. (I am using Django, btw). The format is like such:
"SOMEVAL__exact", etc., where SOMEVAL is some value that it parses from
the keyword.

Now, I want to call this function, specifying the kwargs. The problem
is that I want to dynamically set the kwargs. For example, I don't want
to hard code in the name like such:
function_call(name__exact="Tom")

I want to generate the keyword on the fly. So, I want to do this:
(assume someVar == 'name' for now)
keyword = someVar + "__exact"

The problem:
I need to know how to get it to use the VALUE of the keyword for the
keyword.

My thoughts:
(1) Maybe I can override the kwargs parameter by doing such:
function_call(kwargs={keyword:"Tom"})
*loud buzzer* Nope. (as expected)

function_call(**{keyword:"Tom"})

</F>
 
C

Carsten Haese

Ok, so here is my situation:

Let's assume I have a function that makes good use of the kwargs
parameter. It requires that there is a certain "format" for the kwargs
keywords. (I am using Django, btw). The format is like such:
"SOMEVAL__exact", etc., where SOMEVAL is some value that it parses from
the keyword.

Now, I want to call this function, specifying the kwargs. The problem
is that I want to dynamically set the kwargs. For example, I don't want
to hard code in the name like such:
function_call(name__exact="Tom")

I want to generate the keyword on the fly. So, I want to do this:
(assume someVar == 'name' for now)
keyword = someVar + "__exact"

The problem:
I need to know how to get it to use the VALUE of the keyword for the
keyword.


My thoughts:
(1) Maybe I can override the kwargs parameter by doing such:
function_call(kwargs={keyword:"Tom"})
*loud buzzer* Nope. (as expected)

(2) Maybe I can just pass it in:
function_call(keyword="Tom")
Nope! (as expected, it tries to use 'keyword' as the keyword)

The correct answer is behind door number 3:

some_dict = {keyword: "Tom"}
function_call(**some_dict)

HTH,

Carsten.
 
T

tzellman

Well, I knew about the apply() function, but totally forgot to use it.
It worked.

Example:
apply(function_call, (), {keyword: "Tom"})
 
T

tzellman

Thanks. That also worked. I will use that, since apply() is deprecated
as of 2.3.

Thanks!
-Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top