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! 4 1621
On Wed, 2006-02-15 at 11:40, tz******@gmail.com wrote: 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.
Well, I knew about the apply() function, but totally forgot to use it.
It worked.
Example:
apply(function_call, (), {keyword: "Tom"}) tz******@gmail.com wrote: 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>
Thanks. That also worked. I will use that, since apply() is deprecated
as of 2.3.
Thanks!
-Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Roy Smith |
last post by:
I've got a function that takes a couple of optional keyword arguments.
I want to check to make sure I didn't get passed an argument I didn't
expect. Right now I'm doing:
conversion = None
drop...
|
by: Christos TZOTZIOY Georgiou |
last post by:
Hi all,
this post contains at the end a handy module that I've used quite often
when I wanted to analyse the occasional complex expression and how it
was to be evaluated.
The function...
|
by: Ryan |
last post by:
How can I use the value of a variable to represent a keyword in a function call?
For example:
def foo(**kwargs):
kwargs = **kwargs
item = "temperature"
|
by: John Z. Smith |
last post by:
Hi,
I want to subclass a class (more precisely, optparse.Option). that looks
like
class Option:
def __init__(self, *opts, **attrs):
do_something()
I want to do something in my __init__...
|
by: lbolognini |
last post by:
Hi all,
I have a very long list of parameters coming from a web form to my
method foo(self, **kwargs)
I would like to avoid manually binding the variables to the values
coming through the...
|
by: JonathanB |
last post by:
Ok, this is probably definitely a newbie question, but I have looked
all over the Python library reference material and tutorials which I
can find online and I cannot find a clear definition of...
|
by: Steven D'Aprano |
last post by:
I'm writing a factory function that needs to use keywords in the produced
function, not the factory. Here's a toy example:
def factory(flag):
def foo(obj, arg):
if flag:
# use the spam keyword...
|
by: Sean DiZazzo |
last post by:
Why is the following not working? Is there any way to get keyword
arguments working with exposed XMLRPC functions?
~~~~~~~~~~~~~~~~ server.py
import SocketServer
from SimpleXMLRPCServer import...
|
by: Amir |
last post by:
How do you filter keyword arguments before passing them to a function?
For example:
def f(x=1): return x
def g(a, **kwargs): print a, f(**kwargs)
In : g(1, x=3)
1 3
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |