Angus Comber
Angus Comber

Reputation: 9708

How can I pass parameters into these functions called via dictionary lookup

I need to be able to get at all the parameters passed into my function, make_choice. But on line 25 (requesterverb) I am getting runtime error:

requester[verb](kwargs)
TypeError: doPUT() takes exactly 0 arguments (1 given)

What am I doing wrong?

Here is my code:

#switch using dictionary
def make_choice(verb, **kwargs):
    def doGET(**kwargs):
        print "Doing GET"
        for key in kwargs:
            print "another keyword arg: %s: %s" % (key, kwargs[key])

    def doPUT(**kwargs):
        print "Doing PUT"
        for key in kwargs:
            print "another keyword arg: %s: %s" % (key, kwargs[key])

    def doDELETE(**kwargs):
        print "Doing DELETE"  

    def doPOST(**kwargs):
        print "Doing POST"

    def doPATCH(**kwargs):
        print "Doing PATCH"  

    requester = { 'GET': doGET, 'PUT': doPUT }

    requester[verb](kwargs)


make_choice(verb='PUT',param1='param1',param2='param2',param3='param3')
print "done"

Upvotes: 3

Views: 716

Answers (2)

Jan Spurny
Jan Spurny

Reputation: 5537

You forgot ** before kwargs in:

requester[verb](**kwargs)

Upvotes: 3

Shaac
Shaac

Reputation: 195

In make_choice scope, kwargs is a dictionary containing your paramX vaules and keys. So in this function scope, kwargs is nothing more than a dictionary.

Just remove the ** before the kwargs in the arguments of your doXXX functions, and it will work.

Upvotes: 1

Related Questions