Reputation: 10376
Using Python 2.6, I want to pass args
to the class but I'm getting the following error.
Traceback (most recent call last):
File "./myscript.py", line 95, in <module>
args = get_parser().parse_args()
File "./myscript.py", line 22, in get_parser
generate_mutex.set_defaults(func=ns_calls.generate_lbvs_bindings)
NameError: global name 'ns_calls' is not defined
Main script:
# myscript.py
from myclassfile import *
def get_parser():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="subparser_name")
generate = subparsers.add_parser('generate')
generate_mutex = generate.add_mutually_exclusive_group(required=True)
generate_mutex.add_argument('-g', '--servicegroupname', help='The servicegroup name.',metavar='<servicegroup name>')
generate_mutex.add_argument('-l', '--lbvservername', help='The virtual erver name to which the service is bound.',metavar='<lbvserver name>')
generate_mutex.set_defaults(func=ns_calls.generate_lbvs_bindings)
if __name__ == "__main__":
args = get_parser().parse_args()
args.func(args)
ns_calls = NetscalerCalls(args)
My module file:
# myclassfile.py
class NetscalerCalls(object):
def __init__(self, args):
self.lbvservername = args.lbvservername
self.servername = args.servername
Upvotes: 1
Views: 357
Reputation: 9631
You didn't put the args in here:
ns_calls = NetscalerCalls()
^
This is invalid according to the class you created, where __init__
requires (self, args)
.
When you initiate the class, it passes in self
automatically. So you need to pass in the args when initiating the class.
For example:
# myscript.py
from myclassfile import *
...
...
if __name__ == "__main__":
args = get_parser().parse_args()
args.func(args)
ns_calls = NetscalerCalls(args)
If you try to pass in args before it is declared, it will fail
That is why I have placed the variable declaration for ns_calls
underneath the declaration for args
Upvotes: 3
Reputation: 14520
Your init requires you to pas an args parameters, but when you do the line ns_calls = NetscalerCalls()
you aren't passing that parameter in so it complains. You would have to that line after you have the args object you want then do ns_calls = NetscalerCalls( args )
Upvotes: 1