I am using argparse in my python program and I want to call a method when I run my software like this :
$ python __init__.py foo
It is easy if I want to call a function instead of a method :
def foo(args):
pass
def main():
foo_parser.set_defaults(func=foo)
if __name__ == "__main__":
main()
How can I replace “func=foo” by “func=MyClass.my_method” ?
You just refer to the method instead of the function. Yup. It’s that easy. Instead of
You do
Done!