I am relatively new to Python and I am trying to solve the following problem at the moment. I am writing a script which parses command line arguments with argparse. Besides some other things I have a verbosity flag, which I want to make available for some objects. Currently I am passing it to the constructor and I am wondering if there is a more elegant way for doing this.
class MyClass(object):
def __init__(self, verbosity=False):
self.verbosity = verbosity
def do_something(self):
if self.verbosity:
print("Doing something ...")
.
import argparse
import myclass
def main():
parser = argparse.ArgumentParser(description='Testing.')
parser.add_argument('-v', '--verbosity', action='store_true',
help="Produce verbose output")
args = parser.parse_args()
object = myclass.MyClass(verbosity=args.verbosity)
object.do_something()
if __name__ == "__main__":
main()
Any help would really be appreciated.