Python argparse as a function

泄露秘密 提交于 2019-12-03 15:57:26

It looks good, feels good, conforms to Python Zen - so, what's the problem if you didn't see this particular code like that?

Moving a somewhat independent piece of functionality into a subroutine is essential good practice - a manifestation of separation of concerns, to be precise. It's not even about Python.

This is fine. The only suggestion I would make is to allow get_args to take as a parameter (None by default) a list of arguments to pass to parse_args(). This makes testing easier. Also, the import statement should still go at the top of the script, not inside get_args itself.

import argparse

# ...

def get_args(argv=None):
    parser = argparse.ArgumentParser(description="calculate X to the power of Y")
    # ...
    return parser.parse_args(argv)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!