By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,537 Members | 1,444 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,537 IT Pros & Developers. It's quick & easy.

Calling function keywords from a dictionary

P: n/a
Hello!

I'm fooling around with a class wich can map cgi-parameters to
function named parameter.

I have a dic like
dic = {
'action': 'myfunc',
'x': 1,
'y': 2,
'z': 3
}
and I have a method
def myfunc(x, y, z, a=None, b=None)

What I want to do ist calling the method defined in 'action' with
all the named parameter, also instead of
self.myfunct(x=1, y=2, z=3)
I want to do
f = getattr(self, dic['action'])
f(named parameter from the dic)
Its a little like zope does it, but i didnt find it in the Zope Sources ;-)
How can do that?

Thanks, AXEL.
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alexander Straschil wrote:
I have a dic like
dic = {
'action': 'myfunc',
'x': 1,
'y': 2,
'z': 3
}
and I have a method
def myfunc(x, y, z, a=None, b=None)
I assume you forgot the self parameter.
What I want to do ist calling the method defined in 'action' with
all the named parameter, also instead of
self.myfunct(x=1, y=2, z=3)
I want to do
f = getattr(self, dic['action'])
f(named parameter from the dic)

class A: .... def first(self, x, y, z, a=None, b=None):
.... print "calling first"
.... print "x =", x
.... print "a =", a
.... a = A()
d = dict(action="first", x=1, y=2, z=3)
d {'y': 2, 'x': 1, 'z': 3, 'action': 'first'}
# as a sideeffect, pop() removes "action" from the dictionary
# make a copy if that bothers you bm = getattr(a, d.pop("action"))
bm(**d) calling first
x = 1
a = None d["a"] = 99
bm(**d)

calling first
x = 1
a = 99

Peter
Jul 18 '05 #2

P: n/a
Thanks, that was exaclty what I needed, works cool now!

AXEL.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.