0

I have the following structure in my code:

#!/usr/bin/env python
import <something>
.
.
.
from modules import *
from optparse import OptionParser

class Main:
    def __init__(self):
        parser = self.get_argument()
        self.set_argument(parser)

    def my_args(self):
        parser = OptionParser(usage='usage: %prog [options]', version='%prog 1.0.0')
        parser.add_option('-t', '--test', action='store_true', dest='test', default=False,
            help='Help of test.')
        .
        .
        .
        return parser

    def set_args(self, parser)
        options, args = parser.parse_args()
        params = dict(test=options.test, ..., arg100=options.arg100)
        self.start(**params)

    def start(self, **params):
        print params


    def ...:
        ...
    .
    .
    .

if __name__ == '__main__':
    Main()

I want to run start for getting parameters and its values for using in other functions. but:

TypeError: start() takes exactly 2 arguments (1 given)

How can I use kwargs for passing them?

What is best way in order to use parameters in start. I am really new with kwargs.

3
  • How are you calling the start() method? If I use the start() method you've written above, it doesn't error, just prints out an empty dictionary. Commented Mar 9, 2016 at 8:25
  • pardon, yes. you are right. how can I use kwargs for passing them? Commented Mar 9, 2016 at 8:26
  • start is define to take kwargs or keyword arguments, because you put the ** in front of params, then in set_args you define params but you pass test, which is not even declared and you do not pass any keyword params. I don't know if you have another test declared somewhere. You might want to fix those problems. Commented Mar 9, 2016 at 8:26

2 Answers 2

1

**kwargs allows a function to take keywords arguments, and constructs a dictionary

In [3]: def g(**kw):
...:     print(kw)
...:     

In [4]: g(a=1, b=2, c=3)
 {'a': 1, 'c': 3, 'b': 2}

If you want to pass a dictionary to the function, you'll need to unpack the dictionary, like this:

In [5]: d = dict(e=42, f=65)

In [6]: g(**d)
   {'e': 42, 'f': 65}

See http://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/, or the official documentation

Sign up to request clarification or add additional context in comments.

3 Comments

How this code can be changed and use kwargs for passing arguments?
please see update, The error is TypeError: start() takes exactly 2 arguments (1 given) when I call self.start(**params)
What happens when you print params before calling start?
0

The following works for me:

class Test(object):

    def a(self):
        p = {"a":1, "b":2}
        self.start(**p)

    def start(self, **params):
        print(params)


t = Test()
t.a()

result is:

{'a': 1, 'b': 2}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.